Sachin Joshi
Posts: 12
Nickname: aspire
Registered: Jan, 2004
|
|
Re: Read file to JApplet in browser...
|
Posted: Jan 22, 2004 5:50 PM
|
|
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
|
|