The Artima Developer Community
Sponsored Link

Java Answers Forum
Read file to JApplet in browser...

1 reply on 1 page. Most recent reply: Jan 22, 2004 5:50 PM by Sachin Joshi

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Josh Saporl

Posts: 1
Nickname: celly
Registered: Jan, 2004

Read file to JApplet in browser... Posted: Jan 22, 2004 1:49 PM
Reply to this message Reply
Advertisement
Here's my code, see if anyone can figure out why it pops up a window instead of displaying the JTextArea in the browser itself, and why it wont display the main.txt file in the JTextArea... Thanks in advance::::::


Filename: ReaderX.class

import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.net.*;
import java.io.*;

/**
* Creates a GUI application that displays scrollable text file
* inner class
* @author Sean Brown
* @version 1.0
*/

public class ReaderX extends JApplet
{
private static JTextArea display;
private String write;
private static String sContent;

/**
* Constructor that creates the GUI
*/

public ReaderX(String indicator)
{
write = getFileData(indicator);
JFrame f = new JFrame ("ReaderX");
Container cp = f.getContentPane();
cp.setLayout (new FlowLayout());
display = new JTextArea(write);
JScrollPane jcpDisplay = new JScrollPane(display);
cp.add(jcpDisplay);
f.pack();
f.setVisible(true);
}

public static String getFileData(String sFileName){
String sTheFileContent = "";
try{
//opens the file
FileReader fr = new FileReader(sFileName);
//this declares the char array
char caData[] = new char[1000000];
//this gets the data, each char into the char array
int i = fr.read(caData);
//convert char array to string
sContent = new String(caData, 0, i);
//close the file
fr.close();
}
catch(Exception e) {sContent = null;}
return sContent;
}
}


Filename: ReaderApplet.class
import java.awt.*;
import javax.swing.*;

public class ReaderApplet extends JApplet
{

public void init()
{
String indicator = this.getParameter("indicator");
Container ps = getContentPane();
ps.add(new ReaderX(indicator));
}
}

Filename: main.txt
blablablabla

Filename: readerX.html

<HTML>
<HEAD><TITLE>ReaderX</TITLE></H EAD>
<BODY>
<CENTER>
<APPLET code="ReaderApplet.class" width=540 height=320>
<PARAM name ="indicator" value ="main.txt">
If you can read this text your browser is not java enabled.
</APPLET>
</CENTER>
</BODY>
</HTML>


Sachin Joshi

Posts: 12
Nickname: aspire
Registered: Jan, 2004

Re: Read file to JApplet in browser... Posted: Jan 22, 2004 5:50 PM
Reply to this message Reply
Hi josh,

After going through your code,there are some suggestions.

You want : It seems you want to read a file on local system and display it in JTextArea. And this JtextArea should be displayed in Applet. right ?

1. You have created 2 applets. You need only 1.
You are calling ReaderApplet through HTML file. This is right. But from ReaderApplet you are again calling ReaderX applet. This is wrong. Actually this is the reason why it is poping up the window.
You do not have to extend the ReaderX class from Applet. Use that class just as a provider of JTextArea component to your main applet. From my point of view, in further steps also, if you need any Swing components, you should create a separate class (may be 1 class for each or 1 class for all components...this is design time decision) that will provide you the components you need thro the public methods like getXXX(). I have changed the code. Please check that.

2. When in applet, if you want to read/write a file on local system, you need to make that applet as SIGNED applet. This is beacuse applet runs in sandbox; inherent security in applet. So, to do this, bundle all your classes in a jar file, sign it and then call the applet from html file.

Below is the code I changed. Compile the classes and open the HTML.

**********

ReaderApplet.java -->

import java.awt.*;
import javax.swing.*;

public class ReaderApplet extends JApplet
{

public void init()
{
String indicator = this.getParameter("indicator");
Container ps = getContentPane();
ReaderX obj = new ReaderX(indicator);
ps.add(obj.getTextArea());
}
}
************
ReaderX.java -->

import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.net.*;
import java.io.*;

/**
* Creates a GUI application that displays scrollable text file
* inner class
* @author Sean Brown
* @version 1.0
*/

public class ReaderX //extends JApplet
{
private static JTextArea display;
private String write;
private static String sContent;

/**
* Constructor that creates the GUI
*/

public ReaderX(String indicator)
{
write = getFileData(indicator);
/*JFrame f = new JFrame ("ReaderX");
Container cp = f.getContentPane();
cp.setLayout (new FlowLayout());*/
//display = new JTextArea(write);

display = new JTextArea("Hey Man !!! You need Signed Applet to read the file on the local system through applet.");

JScrollPane jcpDisplay = new JScrollPane(display);
/*cp.add(jcpDisplay);
f.pack();
f.setVisible(true);*/
}

public static String getFileData(String sFileName){
System.out.println("In side getFileData: "+sFileName);
String sTheFileContent = "";
try{
//opens the file
FileReader fr = new FileReader(sFileName);
//this declares the char array
char caData[] = new char[1000000];
//this gets the data, each char into the char array
int i = fr.read(caData);
//convert char array to string
sContent = new String(caData, 0, i);
//close the file
fr.close();
}
catch(Exception e) {sContent = null;}
System.out.println("FileData is : "+sContent);
return sContent;
}

public JTextArea getTextArea() {
return display;
}
}

****************
main.txt -->
blablablabla

*********
readerX.html -->

<HTML>
<HEAD><TITLE>ReaderX</TITLE></H EAD>
<BODY>
<CENTER>
<APPLET code="ReaderApplet.class" width=540 height=320>
<PARAM name ="indicator" value ="main.txt">
If you can read this text your browser is not java enabled.
</APPLET>
</CENTER>
</BODY>
</HTML>

********* *****

Feel free to comment.

Sachin

Flat View: This topic has 1 reply on 1 page
Topic: AutevoSpaces - Worlds First SSI Clustered JavaSpaces Previous Topic   Next Topic Topic: food chain simulation in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use