Moderator (MDR): Tim Rohaly of the MageLang Institute
MDR-rohaly Hello all, and welcome to the Office Hours for the Swing Short Course.
The primary purpose of this discussion session is to answer any questions you may have about the short course. If time permits, general Swing questions may also be posted.
My name is Tim Rohaly. I am from the MageLang Institute (we wrote the course material) and will be answering your questions today.
suhas: I have Netscape 4.03. The applet works fine with Swing components, but if I use the JTree component, I get a message in JavaTM console.
UIDefaults.getUI() failed: no component UI class for:
com.sun.java.swing.JTree(0,0,0x0,invalid)
and obviously it never appears.
MDR-rohaly Do you have a short piece of code I could try out? Doing what you want should work, therefore the problem must be a CLASSPATH or a Netscape bug. It would help if I could look at it myself.
hfelder: I must be missing something with Swing. How do I add something to a JList that I've already created?
MDR-rohaly The Swing components use the MVC design pattern, which is a little different than the way things work with the AWT. The JList component as a data model (the M of MVC) associated with it. This data model generates events when the contained data changes. JList implements ListModelListener , and the ListModel generates a ListModelEvent when the model is changed. The model is changed through a controller (the C in MVC) and the state of the JList is displayed through its view (the V in MVC). If you use the DefaultListModel , then you can add elements to the JList using DefaultListModel.addElement(Object) . MVC will be explained in detail in part II of the Swing short course, which will be available on the JDC in the near future.
hfelder: Do you know where I can find information on the MVC, besides what is provided in the documentation that comes with Swing?
MDR-rohaly Well, as I said, Part II of the Swing Short Course will cover this in detail, but it's not available yet. Another place to look for general information on MVC is in an OO book, like Design Patterns by Gamma, Helm, Johnson, and Vlissides. Published by Addison-Wesley, 1995.
But Design Patterns contains general information. For specific information on how MVC relates to Swing, look at the Swing API docs or wait for Part II of this short course.
suhas: Here is a code snippet that gives the error, I don't think that there is a CLASSPATH problem because it works fine for other Swing components.
public class MyTree extends Applet{
public void init(){
DefaultMutableTreeNode root =
new DefaultMutableTreeNode("ROOT");
root.add(new DefaultMutableTreeNode("BRANCH1"));
root.add(new DefaultMutableTreeNode("BRANCH2"));
add(new JLabel("It works"));
add(new JTree(root));
}
}
Rohaly, here is one funny thing, I don't know if its a bug in appletviewer or Swing. (Assume I have created a tree structure as in the previous code and the object is root)
Use Appletviewer
Case1:
JTree tree = new JTree(root);
add(new JLable("It works"));
add(tree);
(Tree won't appear in the applet)
Case 2:
JTree tree = new Jtree(root);
add(tree);
add(new JLabel("It Works"));
(This works and tree appears in the applet) It doesn't make any sense to me, but it works. One more funny thing: the tree appears in first case if you use the default constructor of tree, so you just replace the first line in case1 with JTree tree = new JTree() and voila, it works.
MDR-rohaly Looking at suhas' code, I also find that it fails. It works if I create a frame and instantiate his applet in my frame, and it works in appletviewer, but it fails in Netscape 4.04 for me with an error:
"NoSuchMethodError: java.awt.Component: method enableEvents (J)V not found
I don't know the cause of this. For what it's worth, I can't get a simple JButton to work in Netscape 4.04 either, although it does run standalone and in appletviewer. I think I have to attribute this to a Netscape bug right now.
Bediako: I have a couple of questions about Swing. I assume that the JDKTM 1.0.2 API does not support Swing. Am I correct?
The second question has to do with the JavaTM Web ServerTM and whether or not I can import swing.jar as a Bean. I've tried to do so, but I get an error. Something about the swing.jar class is not introspectable. This stops me from creating a Swing palette in JWS. I understand that this is listed as a low-priority bug fix on Bug Parade, however I would like to verify Swing's incorporation with the JWS. Is there a way for me to get around (fix) this? Or will there be a fix posted soon? This seems to be a serious problem. RAD is not possible if Swing is not Bean-compliant.
MDR-rohaly You are right that JDK 1.0.2 is incompatible with Swing. And yes, you should be able to import swing.jar as a Bean into JWS. I do not use JWS, but I have done this in Borland's JBuilder. I think I heard that swing.jar will not import into JWS because there is a class file missing, and JWS does some strict checks on the contents of the JAR file. I thought this was a Swing 0.5 problem that was fixed in wing 0.5.1.
All Swing components are valid Beans. They are 100% Beans-compliant right now. What you are seeing is not a fundamental problem, but one in either how the JAR file was made or how JWS parses the JAR. To get around it, you can just fix the jar file so that JWS will read it in. That is, look at the JWS error message. If a .class file is missing, create a dummy .class file and add it to the JAR file. If the manifest is wrong, fix the manifest and remake the jar.
hfelder: Last question. I'm developing an applet/application. When I run it as an application I have no problem. However, when I try to run it as an applet it works until it gets to a swing component and I get a java.lang.NoClassDefFoundError: com/sun/java/swing/JFrame error. I'm also unable to get the applet to run through Netscape 4.04. I can run the Swing demos with no problem. Any suggestions?
MDR-rohaly The only time I have ever seen that NoClassDefFoundError is when CLASSPATH is wrong. swing.jar and rose.jar must be in your CLASSPATH. However, I am puzzled by the fact that it works for you as an application and not an applet. I can only guess that inadvertently forgot to set the CLASSPATH during testing.
One hour is up. Chech the schedule for future sessions, then save up your questions, and come back! Thank you all for coming, I hope I was able to answer some of your questions.
|