Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

REWARD : pass a2a parameter, involves using getCodeBase()

 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, hope I've better response here this time.
I am offering a reward of US$20 for any assistance which helps me solve this problem.

Requirement
===========

I have 3 applets: a1 calls a2, a2 inturn calls a3 . All applets tested and compiled okay.
Need to pass one parameter from a2 to a3 in order for a3 to pop-up and display an html formatted text file. The parameter is of type String, headerText.
a3 must initialize getCodeBase() in order to read in an html file called "ForestFire.html", to be dislayed on the JEditorPane.
a2 is "DescriptionTable.java"
a3 is "ExplanatoryText.java"

Problems
========

On inserting the method ExplanatoryText(String headerTxt) in the *modified* a3, we cannot initialize url with getCodeBase() or getDocumentBase(). JConsole reports null for both. Therefore cannot read in html-formatted text file.
However, without ExplanatoryText(String headerTxt), we can initialize url and display the html-formatted file.
Please see code. The original version of a2 would be ideal *if* we can pass a parameter to it.

Codes
=====

Following is the a2 code snippet which calls a3 "ExplanatoryText.java":

Following is the *modified* ExplanatoryText.java. compiles okay.
I've inserted the sub-class ExplanatoryText(String headerTxt) so that it can receive one parameter.
When I run it, java console tells me this:

The headerTxt is : ForestFire
codeBase initialized to : null
headerTxt is equal to : ForestFire
URL for the document : null
exception: invalid url
java.io.IOException: invalid url
.........at javax.swing.JEditorPane.setPage(Unknown Source)

(1) How do I get the url so that I can display the html-formatted text file?
(2) WHy has the modified a2 stopped initializing the url (see java console output)?
(3) To claim $, provide a snailmail address for cash. Will pay only in US$.
TIA :-)
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. You've missed out some of the important lines of code (the start of the class definitions, where you say what classes and/or interfaces your class extends and implements, and the HTML which indicates which class is the actual Applet), so I'm going to have to guess a little.
It looks very much like you are assuming that the "init" method of your "ExplanatoryText" object will be called by the applet container, but that class doesn't actually look much like an applet to me. The "init" method is only called on the top-level Applet class mentioned in the HTML(in this case, that seems to be TestText or whatever your first snippet is called).
So, it seems that because your "init" is not being called in ExplanatoryText, that your codebase stuff is not being set up correctly. I can see two likely solutions for this. One is to move the code currently in ExplanatoryText.init() to the ExplanatoryText constructor. Another is to pass in the codebase fetched during the main Applet init() as an extra parameter to the ExplanatoryText constructor.
Personally, I favor the second option, as it reduces the muber of times the "getCodeBase()" is called, and will continue to work even if the subsidiary code is moved into a jar file, or fetched from a different URL to the main applet code.
ExplanatoryText.java:

Applet

I hope this works, let us know how you get on.
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I do see where u r coming from.
Please be assured that your response is very much *appreciated*.
However if I were to getCodeBase() from a1 i.e. the top level, then I'd be passing url from a1 to a2, and from a2 to a3. Good practice would suggest that we get url when and where it is needed i.e. at a3, ExplanatoryText.java
I would like to stick with this practice if *possible* because there are a lot of codes here.
Here is the codes again, including the necessary bits I've omitted for your accessment:
Code
====
This is the code snippet from a1, which calls a2 on mouseclick
(NB on click, not on release etc)

Following is the a2 code snippet which calls a3 "ExplanatoryText.java":

Following is the original code for ExplanatoryText.java It fulfils the intended purpose.
Now I need to pass a parameter to it in order to expand on the logic:

Following is the *modified* ExplanatoryText.java. compiles okay.
I've inserted the sub-class ExplanatoryText(String headerTxt) so that it can receive one parameter.
When I run it, java console tells me this:

The headerTxt is : ForestFire
codeBase initialized to : null
headerTxt is equal to : ForestFire
URL for the document : null
exception: invalid url
java.io.IOException: invalid url
.........at javax.swing.JEditorPane.setPage(Unknown Source)

[code]
public class ExplanatoryText extends JApplet
{
Font txtFont = new Font("Monospaced", Font.PLAIN,11);
...lots of java code...
private JScrollPane fileScrollPnl;
private Container content;
private String codeBase, insType, urlString;
private URL url;
public void init()
{
try
{ // NB keep all description files in sub-directories relative to this applet...
// change dir/subdir/fileName, re-compile and u see it works...
url = new URL(getCodeBase() + "ForestFire.html");
} catch (IOException iox)
{System.out.println("exception: " + iox.getMessage());
iox.printStackTrace();
}
}
public ExplanatoryText(String headerTxt) throws IOException
{
insType = headerTxt;
System.out.println("The headerTxt is : " + headerTxt);
System.out.println("codeBase initialized to : " + url);
//
if (headerTxt.equals("ForestFire"))
{ // insert logic here
System.out.println("headerTxt is equal to : " + headerTxt);
}
DisplayHTML displayHtml = new DisplayHTML(headerTxt);
}// end of method ExplanatoryText
//
public class DisplayHTML extends JFrame
{
public DisplayHTML(String headerTxt)
{
System.out.println("URL for the document : " + url);
JFrame f = new JFrame("Explanatory Note : "+ headerTxt + url);
f.setSize(800,200);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//
content = f.getContentPane();
content.setBackground(Color.white);
content.setLayout(new BorderLayout());
try
{ JEditorPane edPane = new JEditorPane(url);
edPane.setEditable(false);
fileScrollPnl = new JScrollPane(edPane);
fileScrollPnl.setPreferredSize(new Dimension(250,380));
} catch(IOException iox)
{ System.out.println("exception: " + iox.getMessage());
iox.printStackTrace();
}
content.add("Center", fileScrollPnl);
//f.add(content); // already extending JFrame, no need to do this...
f.setVisible(true);
}// end of method DisplayHTML()
}// end of class DisplayHTML
}// end of class ExplanatoryText
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There definately seems to be some confusion as to the difference between an applet (a small application which runs in a browser) and an Applet (a class with a few particular methods which can be called by the an applet container in a browser). You mentioned initially that you have three applets (a1, a2, a3), but from looking at your code it seems much more likely that you have one applet comprising three classes. the class CCYTable (a2) is not an Applet, and although ExplanatoryText (a3) is marked as an Applet, you seem to create it in your own code rather than letting the applet container in the browser manage it.
Most applets that you see on a browser screen consist of lots of objects of different classes working together. Only one of the classes is an Applet, and that is the "entry point" class mentioned in the HTML. If you have worked with command-line Java programs in the past, this is equivalent to the class which has the "main" method. The rest of the classes can be considered as "helper classes" or "business logic". These other classes are the ones that do the work of the system. The actual class marked as an Applet is mainly there so that the browser has "init", "start", "stop", and "destroy" methods to call.
When the Applet Container in a browser sees HTML indicating that an applet is present, it creates an instance of that class by calling a constructor with no arguments, then calls the init() method. When the browser is ready to run the applet it calls the start() method, if it needs to stop ths applet (for example because the user has navigated to a different page) it calls the stop() method. When the browser has finished with the applet (for example when the browser window is closed) it calls the destroy() method.
Note that because the applet container always calls a constructor with no arguments, any of your Applet constructors which take arguments will never be called. If you mark a class an Applet, but don't provide a constructor with no arguments it is unlikely to work at all.
Typically the actual code in an Applet class is kept quite short. Create a few objects of the helper classes in init(), maybe pass in a few parameters from the applet context. Then make calls to methods of these created objects in the rest of the lifecycle methods. You should not put any code in the constructor. Anything you would normally do in a construct you should really consider doing in init() instead.
I'm guessing that the class TopLvlMenu is the actual Applet, the only one that you mention in your HTML (I'd still like to see the HTML snippet where you reference these applet(s), to make sure of all this). If so, this is the only one that the browser will manage, and therefore the only one that will have its "init" method called. All the other classes in this system are just regular Java helper classes, created and managed by your own code. They won't have an "init" method called by the container; they won't have access to any data in the applet context unless things are passed in to them from the Applet.
Good practice would suggest that we get url when and where it is needed i.e. at a3, ExplanatoryText.java
I'm not sure I agree with this. In general, if a value is complicated or expensive to retrieve it makes sense to retrieve it only once, and share the value. Passing a String value to a method is something Java finds very easy to do, fetching the codebase may involve all sorts of difficult things including communicating with a containing browser or even back to the server.
This is especially true in this case, as (as far as I can tell) the class ExplanatoryText is only marked s "extends JApplet" is so that you don't get a compiler error when you call getCodeBase(). This should be hinting to you that ExplanatoryText is not really an Applet, just a "helper class", and that calling getCodeBase() won't get you any useful information anyway.
Did that make any sense?
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for spending so much time on my issue.
To put our theory to test, I've slightly re-designed the flow of the application.
a1 = TopLvlMenu.java calls a2;
a2 = CCYFrame.java, the current container for testing, calls a3;
a3 = CCYTable.java calls a4
a4 = ExplanatoryText.java
Code
====
Here is the HTML which instantiates a1:

This is the code snippet from a1, which calls a2 on mouseclick

Following java is simply a clean way for me to exit the JFrame, please note the use of ccyTbl.init() to initialize a3.

The point I'm proving is a2 have just invoked a3 which contains its own init() method. The init() must have worked, else the JTable's getToolTipText() couldn't have worked.
Following is the a3 code snippet which calls a4 "ExplanatoryText.java":

The code for ExplanatoryText.java remains unchanged.
Hope to hear from you again.
:-)
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, in the HTML I meant TopLvlMenu rather than cbsMenu.
Here is an alternative strategy I'm working on :
Instead of CCYFrame calling CCYTable, I can try to call CCYTable from TopLvlMenu
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread is now closed, the problem has been escalated.
Thanks again for the time you have spent on my issue. It is much much appreciated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic