need_help_pls
asked on
How do I turn my existing Java code into an applet?
I have a number of Java classes that run a GUI from one main class. How do i go about turning my application into an applet???
what classes do I need to change and how should I go about changing them? any advice would be great?
what classes do I need to change and how should I go about changing them? any advice would be great?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
how can I go about running the application as an applet though?? if all was well?
You'll have to recode the way it does that. They will have to be in the applet's codebase too, i.e. you can't wander outside that applet's own location in the directory tree.
(that is without signing or modification to the security policy)
>>how can I go about running the application as an applet though??
In a browser or appletviewer
In a browser or appletviewer
ASKER
>>how can I go about running the application as an applet though??
In a browser or appletviewer
---- but is there some modification that I need to make to my existing code for this to be feasible?? when I view some exsiting applet code that i have seen, they all seem to run this init() method - what is this about?
In a browser or appletviewer
---- but is there some modification that I need to make to my existing code for this to be feasible?? when I view some exsiting applet code that i have seen, they all seem to run this init() method - what is this about?
Yes, broadly speaking, you can see init() as being equivalent to your application's constructor and main() to the applet's start() method.
ASKER
ok im not having much success with this... i have created my applet and running it from a basic html page but this is what i get:
my gui appears but opens as a separate application, i.e. as if i had run it independently... it is not part of the html page?!? - i assumed it would be
also, my application no longer works... for example i have a textfield that when input is entered and the user hits return, the text is supposed to be processed by the application and changes made to them gui but nothing happens and i do not get any errors either?!?!??! HELP
my gui appears but opens as a separate application, i.e. as if i had run it independently... it is not part of the html page?!? - i assumed it would be
also, my application no longer works... for example i have a textfield that when input is entered and the user hits return, the text is supposed to be processed by the application and changes made to them gui but nothing happens and i do not get any errors either?!?!??! HELP
ASKER
i have probably gone wrong, but i basically just wrote my main method again within the init()
eg:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** A class that allows testing of interpreter, its GUI and the GUIs controller
* (listener).
* @author
*/
public class InterpreterMain extends JApplet
{
public void init(){
Interpreter interpreter = new Interpreter();
InterpreterController controller = new InterpreterController();
InterpreterGUI gui = new InterpreterGUI(controller) ;
controller.setInterpreter( interprete r);
controller.setGUI(gui);
/* GUIs are simply subclasses of JPanel, so we add the GUI
to a JFrame and make the JFrame visible.
*/
JFrame frm = new JFrame("JIN - Java Interpreter for Novice Programmers");
Container contentPane = frm.getContentPane();
contentPane.add(gui);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = gui.makeMenu();
menuBar.add(fileMenu);
frm.setJMenuBar(menuBar);
frm.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0);
}
});
frm.pack();
frm.setSize(700, 550);
//frm.setSize(900, 600);
frm.setVisible(true);
}
public static void main(String[] args)
{
/* Create interpreter, interpreter GUI and controller
*/
Interpreter interpreter = new Interpreter();
InterpreterController controller = new InterpreterController();
InterpreterGUI gui = new InterpreterGUI(controller) ;
controller.setInterpreter( interprete r);
controller.setGUI(gui);
/* GUIs are simply subclasses of JPanel, so we add the GUI
to a JFrame and make the JFrame visible.
*/
JFrame frm = new JFrame("JIN - Java Interpreter for Novice Programmers");
Container contentPane = frm.getContentPane();
contentPane.add(gui);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = gui.makeMenu();
menuBar.add(fileMenu);
frm.setJMenuBar(menuBar);
frm.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0);
}
});
frm.pack();
frm.setSize(700, 550);
//frm.setSize(900, 600);
frm.setVisible(true);
}
}
eg:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** A class that allows testing of interpreter, its GUI and the GUIs controller
* (listener).
* @author
*/
public class InterpreterMain extends JApplet
{
public void init(){
Interpreter interpreter = new Interpreter();
InterpreterController controller = new InterpreterController();
InterpreterGUI gui = new InterpreterGUI(controller)
controller.setInterpreter(
controller.setGUI(gui);
/* GUIs are simply subclasses of JPanel, so we add the GUI
to a JFrame and make the JFrame visible.
*/
JFrame frm = new JFrame("JIN - Java Interpreter for Novice Programmers");
Container contentPane = frm.getContentPane();
contentPane.add(gui);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = gui.makeMenu();
menuBar.add(fileMenu);
frm.setJMenuBar(menuBar);
frm.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0);
}
});
frm.pack();
frm.setSize(700, 550);
//frm.setSize(900, 600);
frm.setVisible(true);
}
public static void main(String[] args)
{
/* Create interpreter, interpreter GUI and controller
*/
Interpreter interpreter = new Interpreter();
InterpreterController controller = new InterpreterController();
InterpreterGUI gui = new InterpreterGUI(controller)
controller.setInterpreter(
controller.setGUI(gui);
/* GUIs are simply subclasses of JPanel, so we add the GUI
to a JFrame and make the JFrame visible.
*/
JFrame frm = new JFrame("JIN - Java Interpreter for Novice Programmers");
Container contentPane = frm.getContentPane();
contentPane.add(gui);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = gui.makeMenu();
menuBar.add(fileMenu);
frm.setJMenuBar(menuBar);
frm.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0);
}
});
frm.pack();
frm.setSize(700, 550);
//frm.setSize(900, 600);
frm.setVisible(true);
}
}
>>opens as a separate application, i.e. as if i had run it independently... it is not part of the html page?!? - i assumed it would be
No. You use a frame, so it'll look like an application.
As far as the operation of the GUI is concerned, i can't comment on the problems there as the processing code doesn't appear in your post.
No. You use a frame, so it'll look like an application.
As far as the operation of the GUI is concerned, i can't comment on the problems there as the processing code doesn't appear in your post.
ASKER
so to make the application just appear on the webpage i should not create a JFrame just add the components to the applet?? should this be done in init() or my main method of InterpreterMain??
The operation of the GUI works perfectly when run as a standard Java application that is why I didnt post the code... when I run it in a browser it doesnt work at all - when i run it in an applet viewwer it works perfectly also... its just that i need it to be able to be displayed in a browser window, i assume the problem stems from the fact that i open files for reading in my application and have not made any allowances for this when using the browser but i am not sure what allowances i need to make?
The operation of the GUI works perfectly when run as a standard Java application that is why I didnt post the code... when I run it in a browser it doesnt work at all - when i run it in an applet viewwer it works perfectly also... its just that i need it to be able to be displayed in a browser window, i assume the problem stems from the fact that i open files for reading in my application and have not made any allowances for this when using the browser but i am not sure what allowances i need to make?
ASKER
so to make the application just appear on the webpage i should not create a JFrame just add the components to the applet?? should this be done in init() or my main method of InterpreterMain??
The operation of the GUI works perfectly when run as a standard Java application that is why I didnt post the code... when I run it in a browser it doesnt work at all - when i run it in an applet viewwer it works perfectly also... its just that i need it to be able to be displayed in a browser window, i assume the problem stems from the fact that i open files for reading in my application and have not made any allowances for this when using the browser but i am not sure what allowances i need to make?
The operation of the GUI works perfectly when run as a standard Java application that is why I didnt post the code... when I run it in a browser it doesnt work at all - when i run it in an applet viewwer it works perfectly also... its just that i need it to be able to be displayed in a browser window, i assume the problem stems from the fact that i open files for reading in my application and have not made any allowances for this when using the browser but i am not sure what allowances i need to make?
>>just add the components to the applet?? should this be done in init()
Yes and yes. AFAIK JApplet uses a BorderLayout by default so just treat your applet like the JFrame
>>
i assume the problem stems from the fact that i open files for reading in my application and have not made any allowances for this when using the browser
>>
Exactly what i would have guessed. The workaround here is
a. Instead of File, create a URL to the resoruce you want to read and get an input stream on it to read it.
b. Make sure the resource is in your applet's codebase
Yes and yes. AFAIK JApplet uses a BorderLayout by default so just treat your applet like the JFrame
>>
i assume the problem stems from the fact that i open files for reading in my application and have not made any allowances for this when using the browser
>>
Exactly what i would have guessed. The workaround here is
a. Instead of File, create a URL to the resoruce you want to read and get an input stream on it to read it.
b. Make sure the resource is in your applet's codebase
ASKER
Sorry - yet another problem:
what do i do for the following... i need to open a html file, which again works fine in the application, but not in the applet???
here is the code i am using currently:
/**
* Creates the help Panel
*
* @return thePanel ... the panel to be created
*/
public JPanel createHelp()
{
JPanel thePanel = new JPanel();
thePanel.setBorder(BorderF actory.cre ateTitledB order("Hel p"));
ImageIcon back = new ImageIcon("back.jpg");
JButton backBtn = new JButton(back);
helpLbl = new JLabel(HELP_SELECT);
try
{
ep = new JEditorPane("http://student.cs.ucc.ie/03/ekc1/projectHelp/test.html");
ep.setPreferredSize(new Dimension(375,350));
//ep.setPage(FILENAME);
}
catch (IOException ioe)
{
System.out.println(ioe.get Message()) ;
}
ep.setEditable(false);
ep.addHyperlinkListener(ne w HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkE vent he)
{
if (he.getEventType() == HyperlinkEvent.EventType.A CTIVATED)
{
JEditorPane pane = (JEditorPane) he.getSource();
try
{
pane.setPage(he.getURL());
}
catch (IOException ioe)
{
System.out.println(ioe.get Message()) ;
}
}
}
});
backBtn.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEven t e)
{
try
{
ep = new JEditorPane("http://student.cs.ucc.ie/03/ekc1/projectHelp/test.html#index");
}
catch (IOException ioe)
{
System.out.println(ioe.get Message()) ;
}
}
});
JScrollPane viewer = new JScrollPane(ep);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
thePanel.setLayout(gbl);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(4, 4, 4, 4);
addComp(helpLbl, thePanel, gbl, gbc, 0, 0, 1, 150, 0, 0);
//addComp(backBtn, thePanel, gbl, gbc, 0, 150, 1, 1, 0, 0);
addComp(viewer, thePanel, gbl, gbc, 1, 0, 400, 400, 0, 0);
return thePanel;
}
what do i do for the following... i need to open a html file, which again works fine in the application, but not in the applet???
here is the code i am using currently:
/**
* Creates the help Panel
*
* @return thePanel ... the panel to be created
*/
public JPanel createHelp()
{
JPanel thePanel = new JPanel();
thePanel.setBorder(BorderF
ImageIcon back = new ImageIcon("back.jpg");
JButton backBtn = new JButton(back);
helpLbl = new JLabel(HELP_SELECT);
try
{
ep = new JEditorPane("http://student.cs.ucc.ie/03/ekc1/projectHelp/test.html");
ep.setPreferredSize(new Dimension(375,350));
//ep.setPage(FILENAME);
}
catch (IOException ioe)
{
System.out.println(ioe.get
}
ep.setEditable(false);
ep.addHyperlinkListener(ne
{
public void hyperlinkUpdate(HyperlinkE
{
if (he.getEventType() == HyperlinkEvent.EventType.A
{
JEditorPane pane = (JEditorPane) he.getSource();
try
{
pane.setPage(he.getURL());
}
catch (IOException ioe)
{
System.out.println(ioe.get
}
}
}
});
backBtn.addActionListener(
{
public void actionPerformed(ActionEven
{
try
{
ep = new JEditorPane("http://student.cs.ucc.ie/03/ekc1/projectHelp/test.html#index");
}
catch (IOException ioe)
{
System.out.println(ioe.get
}
}
});
JScrollPane viewer = new JScrollPane(ep);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
thePanel.setLayout(gbl);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(4, 4, 4, 4);
addComp(helpLbl, thePanel, gbl, gbc, 0, 0, 1, 150, 0, 0);
//addComp(backBtn, thePanel, gbl, gbc, 0, 150, 1, 1, 0, 0);
addComp(viewer, thePanel, gbl, gbc, 1, 0, 400, 400, 0, 0);
return thePanel;
}
Is your applet being served from http://student.cs.ucc.ie?
ASKER
yeah.. applet is served from student.cs.ucc.ie
Comment out the ImageIcon bit and let me know what happens
ASKER
the same - makes no difference...
when i hit the help btn in the gui my controller calls the above method create help, when i hit the help btn i get errors along the lines of :
java.security.AccessContro lException : access denied (java.net.SocketPermission s
tudent.cs.ucc.ie resolve)
at java.security.AccessContro lContext.c heckPermis sion(Acces sControlCo nte
xt.java:270)
at java.security.AccessContro ller.check Permission (AccessCon troller.ja va:
401)
when i hit the help btn in the gui my controller calls the above method create help, when i hit the help btn i get errors along the lines of :
java.security.AccessContro
tudent.cs.ucc.ie resolve)
at java.security.AccessContro
xt.java:270)
at java.security.AccessContro
401)
OK. I think you'll have to eliminate the potential problems one at a time, so comment out one thing at a time.
Try empty constructor and getStream on the editor pane
Try empty constructor and getStream on the editor pane
ASKER
ok:
sorry bout this its just that this is part of my final year project that is due to present on thursday so im kind of scared now:
my applet still doesnt work in the browser i have made the following changes below, and still all that happens is that it loads but wont work...
also, i still cannot get the help button to work in the applet viewer, however when i run it in the browser the panel will display but none of the links work????
im really confused!?!?! any help would be great? emergency...
public InputStream getData(String filename) throws Exception
{
InputStream is = null;
try
{
//The applet is being run locally
is = new FileInputStream(filename);
}
catch (SecurityException se)
{
//The applet is being run across the internet
//getDocumentBase();
//URL current = getDocumentBase();
URL current = new URL("http://student.cs.ucc.ie/03/ekc1/project/GUIstuff/run.html");
URL cgi = new URL(current.getProtocol(),
current.getHost(), current.getPort(),
current.getPath()+"/cgirea d.cgi?file name="+fil ename);
URLConnection con = cgi.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Co ntent-type ","text/pl ain");
PrintStream out = new PrintStream(con.getOutputS tream());
out.print(filename);
out.flush();
out.close();
is = con.getInputStream();
}
return is;
}
/**
* Checks that the variable name supplied is valid
* @return boolean
*/
public boolean checkName(String theText)
{
try
{
BufferedReader validName = new BufferedReader(new InputStreamReader(getData( "validChar .txt")));
String letter = new String();
while(validName.ready())
{
StringTokenizer letterStk = new StringTokenizer(validName. readLine() , "\n");
letter = (String)letterStk.nextToke n();
if(theText.startsWith(lett er))
{
return true;
}
}
if(!validName.ready())
{
validName.close();
validName = new BufferedReader(new InputStreamReader(getData( "validChar .txt")));
}
validName.close();
return false;
}
catch(Exception ioe)
{
System.out.println("File not found");
ioe.printStackTrace();
return false;
}
}
where cgiread.cgi is:
#!/usr/bin/perl
@values = split(/&/,$ENV{'QUERY_STRI NG'});
foreach $i (@values) {
($varname, $mydata) = split(/=/,$i);
if ($varname == "filename")
{
$filename = $mydata;
}
}
open(INFO, $filename);
@array=<INFO>;
close (INFO);
print "Content-type: text/plain\n\n";
foreach $line (@array)
{
print $line;
}
exit;
sorry bout this its just that this is part of my final year project that is due to present on thursday so im kind of scared now:
my applet still doesnt work in the browser i have made the following changes below, and still all that happens is that it loads but wont work...
also, i still cannot get the help button to work in the applet viewer, however when i run it in the browser the panel will display but none of the links work????
im really confused!?!?! any help would be great? emergency...
public InputStream getData(String filename) throws Exception
{
InputStream is = null;
try
{
//The applet is being run locally
is = new FileInputStream(filename);
}
catch (SecurityException se)
{
//The applet is being run across the internet
//getDocumentBase();
//URL current = getDocumentBase();
URL current = new URL("http://student.cs.ucc.ie/03/ekc1/project/GUIstuff/run.html");
URL cgi = new URL(current.getProtocol(),
current.getHost(), current.getPort(),
current.getPath()+"/cgirea
URLConnection con = cgi.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Co
PrintStream out = new PrintStream(con.getOutputS
out.print(filename);
out.flush();
out.close();
is = con.getInputStream();
}
return is;
}
/**
* Checks that the variable name supplied is valid
* @return boolean
*/
public boolean checkName(String theText)
{
try
{
BufferedReader validName = new BufferedReader(new InputStreamReader(getData(
String letter = new String();
while(validName.ready())
{
StringTokenizer letterStk = new StringTokenizer(validName.
letter = (String)letterStk.nextToke
if(theText.startsWith(lett
{
return true;
}
}
if(!validName.ready())
{
validName.close();
validName = new BufferedReader(new InputStreamReader(getData(
}
validName.close();
return false;
}
catch(Exception ioe)
{
System.out.println("File not found");
ioe.printStackTrace();
return false;
}
}
where cgiread.cgi is:
#!/usr/bin/perl
@values = split(/&/,$ENV{'QUERY_STRI
foreach $i (@values) {
($varname, $mydata) = split(/=/,$i);
if ($varname == "filename")
{
$filename = $mydata;
}
}
open(INFO, $filename);
@array=<INFO>;
close (INFO);
print "Content-type: text/plain\n\n";
foreach $line (@array)
{
print $line;
}
exit;
OK. The first thing you must do is to pay very close attention to the stack trace of any Exceptions. Check the plugin console
ASKER
im so sorry this is going to soud like a very silly question, it is just that i have never written/used any applet before, but how do i check the console when i am running the applet from a browser?
so not sure how to check what the problem is when i run it in the browser??
but, when i run it in the appletviewer i dont get any errors when running it until i hit the help button which gives: (i dont understand this error really - )
java.security.AccessContro lException : access denied (java.net.SocketPermission s
tudent.cs.ucc.ie resolve)
at java.security.AccessContro lContext.c heckPermis sion(Acces sControlCo nte
xt.java:270)
at java.security.AccessContro ller.check Permission (AccessCon troller.ja va:
401)
at java.lang.SecurityManager. checkPermi ssion(Secu rityManage r.java:542 )
at java.lang.SecurityManager. checkConne ct(Securit yManager.j ava:1042)
at java.net.InetAddress.getAl lByName0(I netAddress .java:909)
at java.net.InetAddress.getAl lByName0(I netAddress .java:890)
at java.net.InetAddress.getAl lByName(In etAddress. java:884)
at java.net.InetAddress.getBy Name(InetA ddress.jav a:814)
at sun.net.www.http.HttpClient.<init>(HttpClient .java:282)
at sun.net.www.http.HttpClient.<init>(HttpClient .java:253)
at sun.net.www.http.HttpClient.New(HttpClient.java:321)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:301)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConne
ction.java:469)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection
.java:460)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:562)
at java.net.HttpURLConnection .getRespon seCode(Htt pURLConnec tion.java: 237
)
at javax.swing.JEditorPane.ge tStream(JE ditorPane. java:674)
at javax.swing.JEditorPane.se tPage(JEdi torPane.ja va:392)
at javax.swing.JEditorPane.se tPage(JEdi torPane.ja va:775)
at javax.swing.JEditorPane.<i nit>(JEdit orPane.jav a:249)
at InterpreterGUI.createHelp( Interprete rGUI.java: 362)
at InterpreterGUI.helpFrame(I nterpreter GUI.java:3 37)
at InterpreterController.acti onPerforme d(Interpre terControl ler.java:6 4)
at javax.swing.AbstractButton .fireActio nPerformed (AbstractB utton.java :17
67)
at javax.swing.AbstractButton $ForwardAc tionEvents .actionPer formed(Abs tra
ctButton.java:1820)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Defau ltButtonMo del
.java:419)
at javax.swing.DefaultButtonM odel.setPr essed(Defa ultButtonM odel.java: 257
)
at javax.swing.plaf.basic.Bas icButtonLi stener.mou seReleased (BasicButt onL
istener.java:258)
at java.awt.Component.process MouseEvent (Component .java:5021 )
at java.awt.Component.process Event(Comp onent.java :4818)
at java.awt.Container.process Event(Cont ainer.java :1525)
at java.awt.Component.dispatc hEventImpl (Component .java:3526 )
at java.awt.Container.dispatc hEventImpl (Container .java:1582 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3367)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Contai ner.java:3 359
)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Contain er.java:30 74)
at java.awt.LightweightDispat cher.dispa tchEvent(C ontainer.j ava:3004)
at java.awt.Container.dispatc hEventImpl (Container .java:1568 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3367)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:445)
at java.awt.EventDispatchThre ad.pumpOne EventForHi erarchy(Ev entDispatc hTh
read.java:191)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hre
ad.java:144)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 38)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 30)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:98)
so not sure how to check what the problem is when i run it in the browser??
but, when i run it in the appletviewer i dont get any errors when running it until i hit the help button which gives: (i dont understand this error really - )
java.security.AccessContro
tudent.cs.ucc.ie resolve)
at java.security.AccessContro
xt.java:270)
at java.security.AccessContro
401)
at java.lang.SecurityManager.
at java.lang.SecurityManager.
at java.net.InetAddress.getAl
at java.net.InetAddress.getAl
at java.net.InetAddress.getAl
at java.net.InetAddress.getBy
at sun.net.www.http.HttpClient.<init>(HttpClient
at sun.net.www.http.HttpClient.<init>(HttpClient
at sun.net.www.http.HttpClient.New(HttpClient.java:321)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:301)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConne
ction.java:469)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection
.java:460)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:562)
at java.net.HttpURLConnection
)
at javax.swing.JEditorPane.ge
at javax.swing.JEditorPane.se
at javax.swing.JEditorPane.se
at javax.swing.JEditorPane.<i
at InterpreterGUI.createHelp(
at InterpreterGUI.helpFrame(I
at InterpreterController.acti
at javax.swing.AbstractButton
67)
at javax.swing.AbstractButton
ctButton.java:1820)
at javax.swing.DefaultButtonM
.java:419)
at javax.swing.DefaultButtonM
)
at javax.swing.plaf.basic.Bas
istener.java:258)
at java.awt.Component.process
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
)
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
read.java:191)
at java.awt.EventDispatchThre
ad.java:144)
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
OK, you can test it in appletviewer at first and then move to the browser later. Are you connecting outside your codebase in InterpreterGUI.createHelp( Interprete rGUI.java: 362)?
ASKER
(InterpreterGUI.java:362)
ep = new JEditorPane("http://student.cs.ucc.ie/03/ekc1/projectHelp/test.html");
I run this code when using an appletviwer from my home directory on ocean(student.cs.ucc.ie), then a copy of this folder is uploaded to /csweb/03/ekc1/project/ which is ocean's web repository. projectHelp/test.html is just another directory within that web repository. All the code is identical to what is in my home directory. When I run the code in my home directory, it does, as you can see above access the on-line version of test.html.
When i run in using an appletviewer from my home directory the aforementioned result of clicking the help btn occurs. The java application works.
ep = new JEditorPane("http://student.cs.ucc.ie/03/ekc1/projectHelp/test.html");
I run this code when using an appletviwer from my home directory on ocean(student.cs.ucc.ie), then a copy of this folder is uploaded to /csweb/03/ekc1/project/ which is ocean's web repository. projectHelp/test.html is just another directory within that web repository. All the code is identical to what is in my home directory. When I run the code in my home directory, it does, as you can see above access the on-line version of test.html.
When i run in using an appletviewer from my home directory the aforementioned result of clicking the help btn occurs. The java application works.
ASKER
(InterpreterGUI.java:362)
ep = new JEditorPane("http://student.cs.ucc.ie/03/ekc1/projectHelp/test.html");
I run this code when using an appletviwer from my home directory on ocean(student.cs.ucc.ie), then a copy of this folder is uploaded to /csweb/03/ekc1/project/ which is ocean's web repository. projectHelp/test.html is just another directory within that web repository. All the code is identical to what is in my home directory. When I run the code in my home directory, it does, as you can see above access the on-line version of test.html.
When i run in using an appletviewer from my home directory the aforementioned result of clicking the help btn occurs. The java application works.
ep = new JEditorPane("http://student.cs.ucc.ie/03/ekc1/projectHelp/test.html");
I run this code when using an appletviwer from my home directory on ocean(student.cs.ucc.ie), then a copy of this folder is uploaded to /csweb/03/ekc1/project/ which is ocean's web repository. projectHelp/test.html is just another directory within that web repository. All the code is identical to what is in my home directory. When I run the code in my home directory, it does, as you can see above access the on-line version of test.html.
When i run in using an appletviewer from my home directory the aforementioned result of clicking the help btn occurs. The java application works.
In that case, you haven't followed my suggestion from earlier:
>>Try empty constructor and getStream on the editor pane
>>Try empty constructor and getStream on the editor pane
You should construct the URL argument to that function as follows:
URL url = new URL(getCodeBase(), "somefile");
Find out what getCodeBase() returns so you can form the URL properly.
URL url = new URL(getCodeBase(), "somefile");
Find out what getCodeBase() returns so you can form the URL properly.
ASKER
sorry im not sure that i understand? Im just looking at the apis for getStream and not sure how you mean me to use it..??
you mean the constructor for the EditorPane should be
ep = new JEditorPane();
but how do i go about setting the page then? using getStream?
you mean the constructor for the EditorPane should be
ep = new JEditorPane();
but how do i go about setting the page then? using getStream?
URL url = new URL(getCodeBase(), "somefile");
ep.getStream(url);
i think!
ep.getStream(url);
i think!
ASKER
Z:\Project\GUIstuff>javac *.java
InterpreterGUI.java:365: cannot resolve symbol
symbol : method getCodeBase ()
location: class InterpreterGUI
URL url = new URL(getCodeBase(), "test.html");
^
InterpreterGUI.java:366: getStream(java.net.URL) has protected access in javax.s
wing.JEditorPane
ep.getStream(url);
^
2 errors
InterpreterGUI.java:365: cannot resolve symbol
symbol : method getCodeBase ()
location: class InterpreterGUI
URL url = new URL(getCodeBase(), "test.html");
^
InterpreterGUI.java:366: getStream(java.net.URL) has protected access in javax.s
wing.JEditorPane
ep.getStream(url);
^
2 errors
getCodeBase() needs to be in applet's scope.
Sorry - try setPage with that code instead of getStream
Sorry - try setPage with that code instead of getStream
ASKER
uh-oh! this code is all in my GUI class - I am not creating the applet here. I have a separate class InterpreterMain that orignally invoked the GUI, that now creates the applet.... is there anyway around this??
Pass the applet as a parameter to an appropriate constructor.
ASKER
I will try and explain that better. my applet opens fine in the appletviewer and runs ok - the only problem i have with it in the applet viewer is when i hit the help button. What should happen is that my actionlistener is my controller class will call gui.createHelp() which is the method pasted above that the code snipets are from. this works fine in the applcation but not in the appletviwer. (in the browser the editorpane loads but the link dont work - if that sheds any light on the matter)
ps: I realise this must be very annoying, but i cant say how much i appreciate you taking the time for this - cant ask any class mates as they are all very busy too - so on my own and really stressed now - 1 day to go - arrrrggghhh!!! (maybe i should rethink my career choice!!!?!?!) : )
ps: I realise this must be very annoying, but i cant say how much i appreciate you taking the time for this - cant ask any class mates as they are all very busy too - so on my own and really stressed now - 1 day to go - arrrrggghhh!!! (maybe i should rethink my career choice!!!?!?!) : )
ASKER
this is where i create the applet - how else should i do it if i need to pass it as a param to the gui class (if i picked up what you said correctly??)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** A class that allows testing of interpreter, its GUI and the GUIs controller
* (listener).
* @author Katie Crowley 99037190 ekc1@student.cs.ucc.ie
*/
public class InterpreterMain extends JApplet
{
public void init(){
Interpreter interpreter = new Interpreter();
InterpreterController controller = new InterpreterController();
InterpreterGUI gui = new InterpreterGUI(controller) ;
controller.setInterpreter( interprete r);
controller.setGUI(gui);
/* GUIs are simply subclasses of JPanel, so we add the GUI
to a JFrame and make the JFrame visible.
*/
//JFrame frm = new JFrame("JIN - Java Interpreter for Novice Programmers");
Container contentPane = this.getContentPane();
contentPane.add(gui);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = gui.makeMenu();
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
/*this.addWindowListener(n ew WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0);
}
});
this.pack();*/
this.setSize(700, 550);
//frm.setSize(900, 600);
this.setVisible(true);
}
public static void main(String[] args)
{
/* Create interpreter, interpreter GUI and controller
*/
Interpreter interpreter = new Interpreter();
InterpreterController controller = new InterpreterController();
InterpreterGUI gui = new InterpreterGUI(controller) ;
controller.setInterpreter( interprete r);
controller.setGUI(gui);
/* GUIs are simply subclasses of JPanel, so we add the GUI
to a JFrame and make the JFrame visible.
*/
JFrame frm = new JFrame("JIN - Java Interpreter for Novice Programmers");
Container contentPane = frm.getContentPane();
contentPane.add(gui);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = gui.makeMenu();
menuBar.add(fileMenu);
frm.setJMenuBar(menuBar);
frm.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0);
}
});
frm.pack();
frm.setSize(700, 550);
//frm.setSize(900, 600);
frm.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** A class that allows testing of interpreter, its GUI and the GUIs controller
* (listener).
* @author Katie Crowley 99037190 ekc1@student.cs.ucc.ie
*/
public class InterpreterMain extends JApplet
{
public void init(){
Interpreter interpreter = new Interpreter();
InterpreterController controller = new InterpreterController();
InterpreterGUI gui = new InterpreterGUI(controller)
controller.setInterpreter(
controller.setGUI(gui);
/* GUIs are simply subclasses of JPanel, so we add the GUI
to a JFrame and make the JFrame visible.
*/
//JFrame frm = new JFrame("JIN - Java Interpreter for Novice Programmers");
Container contentPane = this.getContentPane();
contentPane.add(gui);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = gui.makeMenu();
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
/*this.addWindowListener(n
{ public void windowClosing(WindowEvent we)
{ System.exit(0);
}
});
this.pack();*/
this.setSize(700, 550);
//frm.setSize(900, 600);
this.setVisible(true);
}
public static void main(String[] args)
{
/* Create interpreter, interpreter GUI and controller
*/
Interpreter interpreter = new Interpreter();
InterpreterController controller = new InterpreterController();
InterpreterGUI gui = new InterpreterGUI(controller)
controller.setInterpreter(
controller.setGUI(gui);
/* GUIs are simply subclasses of JPanel, so we add the GUI
to a JFrame and make the JFrame visible.
*/
JFrame frm = new JFrame("JIN - Java Interpreter for Novice Programmers");
Container contentPane = frm.getContentPane();
contentPane.add(gui);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = gui.makeMenu();
menuBar.add(fileMenu);
frm.setJMenuBar(menuBar);
frm.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0);
}
});
frm.pack();
frm.setSize(700, 550);
//frm.setSize(900, 600);
frm.setVisible(true);
}
}
Don't worry - these security constrictions are a pain!
>>the only problem i have with it in the applet viewer is when i hit the help button.
My guess is it's down to the way the url is created, a suggested solution to which i've just given
>>the only problem i have with it in the applet viewer is when i hit the help button.
My guess is it's down to the way the url is created, a suggested solution to which i've just given
Maybe here?
>>InterpreterGUI gui = new InterpreterGUI(controller) ;
as
>>InterpreterGUI gui = new InterpreterGUI(controller, this);
>>InterpreterGUI gui = new InterpreterGUI(controller)
as
>>InterpreterGUI gui = new InterpreterGUI(controller,
ASKER
but the applet is created in InterpreterMain??
sorry the confusion is probably due to the fact that i have been looking at this code everyday for the past few months and now im just all over the place!?!?
sorry the confusion is probably due to the fact that i have been looking at this code everyday for the past few months and now im just all over the place!?!?
ASKER
my labs are closing now, so I will check the site again in the morning. hopefully things will be a bit clearer for me then also - thanks again, (goodnight/day whatever part of globe!!!)
>>but the applet is created in InterpreterMain??
The applet reference is in scope here, so you should have no problem:
>>InterpreterGUI gui = new InterpreterGUI(controller, this);
The applet reference is in scope here, so you should have no problem:
>>InterpreterGUI gui = new InterpreterGUI(controller,
ASKER
ok, so i changed the constructor for interpreterGUI to take it the applet and then ran it in the appletviewer with the following results:
java.lang.NullPointerExcep tion
at InterpreterGUI.createHelp( Interprete rGUI.java: 367)
at InterpreterGUI.helpFrame(I nterpreter GUI.java:3 39)
at InterpreterController.acti onPerforme d(Interpre terControl ler.java:6 4)
at javax.swing.AbstractButton .fireActio nPerformed (AbstractB utton.java :17
67)
at javax.swing.AbstractButton $ForwardAc tionEvents .actionPer formed(Abs tra
ctButton.java:1820)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Defau ltButtonMo del
.java:419)
at javax.swing.DefaultButtonM odel.setPr essed(Defa ultButtonM odel.java: 257
)
at javax.swing.plaf.basic.Bas icButtonLi stener.mou seReleased (BasicButt onL
istener.java:258)
at java.awt.Component.process MouseEvent (Component .java:5021 )
at java.awt.Component.process Event(Comp onent.java :4818)
at java.awt.Container.process Event(Cont ainer.java :1525)
at java.awt.Component.dispatc hEventImpl (Component .java:3526 )
at java.awt.Container.dispatc hEventImpl (Container .java:1582 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3367)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Contai ner.java:3 359
)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Contain er.java:30 74)
at java.awt.LightweightDispat cher.dispa tchEvent(C ontainer.j ava:3004)
at java.awt.Container.dispatc hEventImpl (Container .java:1568 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3367)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:445)
at java.awt.EventDispatchThre ad.pumpOne EventForHi erarchy(Ev entDispatc hTh
read.java:191)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hre
ad.java:144)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 38)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 30)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:98)
java.lang.NullPointerExcep
at InterpreterGUI.createHelp(
at InterpreterGUI.helpFrame(I
at InterpreterController.acti
at javax.swing.AbstractButton
67)
at javax.swing.AbstractButton
ctButton.java:1820)
at javax.swing.DefaultButtonM
.java:419)
at javax.swing.DefaultButtonM
)
at javax.swing.plaf.basic.Bas
istener.java:258)
at java.awt.Component.process
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
)
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
read.java:191)
at java.awt.EventDispatchThre
ad.java:144)
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
Then you probably haven't passed the reference on properly to the InterpreterGUI.
ASKER
in InterpreterMain i have changed to :
public class InterpreterMain extends JApplet
{
public void init()
{
Interpreter interpreter = new Interpreter();
InterpreterController controller = new InterpreterController();
InterpreterGUI gui = new InterpreterGUI(controller, this);
.... etc etc
}}
and in InterpreterGUI:
public InterpreterGUI(Interpreter Controller theController, JApplet j)
{
super();
myApplet = j;
/* Store the interpreter controller */
controller = theController;
etc etc...
}
and in InterpreterGUI.createHelp( )
URL url = new URL(myApplet.getCodeBase() , "test.html");
ep.setPage(url);
public class InterpreterMain extends JApplet
{
public void init()
{
Interpreter interpreter = new Interpreter();
InterpreterController controller = new InterpreterController();
InterpreterGUI gui = new InterpreterGUI(controller,
.... etc etc
}}
and in InterpreterGUI:
public InterpreterGUI(Interpreter
{
super();
myApplet = j;
/* Store the interpreter controller */
controller = theController;
etc etc...
}
and in InterpreterGUI.createHelp(
URL url = new URL(myApplet.getCodeBase()
ep.setPage(url);
Is this line 367?
>>URL url = new URL(myApplet.getCodeBase() , "test.html");
>>URL url = new URL(myApplet.getCodeBase()
ASKER
line 367 = ep.setPage(url);
1. Is ep valid?
2. Did you make sure that getCodeBase() returns what you think it does as i suggested before?
2. Did you make sure that getCodeBase() returns what you think it does as i suggested before?
ASKER
code base returns : file:/Z:/Project/GUIstuff/
ep should be valid... i have it declared as
private JEditorPane ep;
ep should be valid... i have it declared as
private JEditorPane ep;
1. I mean is the refence null, obviously
2. Is this url valid?
file:/Z:/Project/GUIstuff/ test.html
2. Is this url valid?
file:/Z:/Project/GUIstuff/
ASKER
IT WORKS NOW!!!!!! not sure why but am so happy that it does!!!!!! maybe i should just leave it run in the appletviewer??? would it be very difficult to get it working in the browser??? thank you sooooo much - im so grateful!!!!!!!!!
>>not sure why
Why not? Surely you know what you've changed!?
Why not? Surely you know what you've changed!?
need_help_pls:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- Points to CEHJ
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
girionis
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
- Points to CEHJ
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
girionis
EE Cleanup Volunteer
ASKER