Applet GUI problems..

You'd think I'd know a thing or three about GUI by now, but I'm stumped.  I've released a project on SourceForge:  JFCML - JFC/Swing XML Markup Language  http://meta-solutions.com/jfcml/index.html .

I'm creating a demo for the website:  http://meta-solutions.com/jfcml/demo/index.html .  I'm running into a little trouble setting the JRootPane more than once.

Basically, the demo lets the user edit a JFCML description file and see the results.  I use javascript to call the runDemo( String ) method defined in the demo class.  The base window container in my package is called JFCMLWindow.  It extends JRootPane and is basically the component that the JFCMLWindowFactory draws on based on the XML. (That's why I need to set the rootpane more than once).

It works the first time(when i call it from init()), but when you click on the button (and call it with javascript), it kills the window.

Here's my demo class (you'll need my jar if you're going to compile it):

import com.metasolutions.jfcml.JFCMLWindow;
import com.metasolutions.jfcml.JFCMLWindowFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.regex.MatchResult;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import java.io.File;

public class test extends JApplet
{
      private JFCMLWindowFactory factory;
      private JFCMLWindow jfcmlw;
      
      public void init()
      {
            factory = new JFCMLWindowFactory();
            runDemo( "<JFCML><JRootPane><ContentPane><JPanel><JLabel Text=\"Hello World\" Background=\"Color(0,255,0)\"/></JPanel></ContentPane></JRootPane></JFCML>");
      }
      
      
      private static final String HEADER =
            "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\r\n\r\n" +
            "<!DOCTYPE JFCML SYSTEM \"http://meta-solutions.com/jfcml.dtd\">\r\n";
      
      public void runDemo( String input ) {
            final String s = HEADER + input;
            try {
                  SwingUtilities.invokeAndWait( new Runnable() {
                        public void run() {
                              createAndShowGUI( s );
                              System.out.println( "ALL DONE!" );
                              System.out.flush();
                        }
                  });
            } catch( Exception e ) {
                  throw new RuntimeException( "Failed to create GUI", e );
            }
            System.out.println( "returning from rundemo()" );
            System.out.flush();
      }
      
      private void createAndShowGUI( String s ) {
            if( jfcmlw != null )
                  remove( jfcmlw );
            invalidate();
            jfcmlw = new JFCMLWindow();
            factory.build( new java.io.CharArrayReader( s.toCharArray() ), jfcmlw );
            System.out.println( "returned from build" );
            System.out.flush();
            add( jfcmlw );
            repaint();
            System.out.println( "set root pane" );
            System.out.flush();
            
            setSize( 300,300 );
            setVisible( true );
      }
      
}
LVL 3
ShawnCurryAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
RuadRauFlessaConnect With a Mentor Commented:
Looks to me like you have a redraw problem there pal

          ninvalidate();
          add( jfcmlw );
          repaint();

try adding a validate() call just after the add( jfcmlw ) call. That should force the applet to do it's layout and redraw itself
0
 
Giant2Commented:
>It works the first time(when i call it from init()), but when you click on the button (and call it with javascript), it kills the window

Any error?
0
 
ShawnCurryAuthor Commented:
Nope - check it out:

http://www.meta-solutions.com/jfcml/demo/index.html

Open your Java console.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ShawnCurryAuthor Commented:
BTW - you need Java 1.4+ for the demo.

Here's what I have now:

import com.metasolutions.jfcml.JFCMLWindow;
import com.metasolutions.jfcml.JFCMLWindowFactory;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import java.io.File;

public class test extends JApplet
{
      private JFCMLWindowFactory factory;
      private JFCMLWindow jfcmlw;
      
      public void init()
      {
            factory = new JFCMLWindowFactory();
            runDemo( "<JFCML><JRootPane><JMenuBar><JMenu Text=\"Hello\" Mnemonic=\"H\"><JMenuItem Text=\"World\"/></JMenu></JMenuBar><ContentPane><JPanel Background=\"Color(255,0,0)\"><JLabel Text=\"Hello World\" Foreground=\"Color(0,255,0)\"/></JPanel></ContentPane></JRootPane></JFCML>");
      }
      
      
      private static final String HEADER =
            "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\r\n\r\n" +
            "<!DOCTYPE JFCML SYSTEM \"http://meta-solutions.com/jfcml.dtd\">\r\n";
      
      public void runDemo( String input ) {
            System.out.println(input);
            final String s = HEADER + input;
            try {
                  SwingUtilities.invokeAndWait( new Runnable() {
                        public void run() {
                              createAndShowGUI( s );
                              System.out.println( "ALL DONE!" );
                              System.out.flush();
                        }
                  });
            } catch( Exception e ) {
                  throw new RuntimeException( "Failed to create GUI", e );
            }
            System.out.println( "returning from rundemo()" );
            System.out.flush();
      }
      
      private void createAndShowGUI( String s ) {
            if( jfcmlw != null )
                  this.removeAll();
            jfcmlw = new JFCMLWindow();
            factory.build( new java.io.CharArrayReader( s.toCharArray() ), jfcmlw );
            System.out.println( "returned from build" );
            System.out.flush();
            invalidate();
            add( jfcmlw );
            repaint();
            
            System.out.println( "set root pane" );
            System.out.flush();
            
            setSize( 300,300 );
            setVisible( true );
      }
      
}
0
 
Giant2Commented:
Mmm. I see an error on my console (1.4.2_05) about the use some swing component.
0
 
ShawnCurryAuthor Commented:
I've noticed, so long as I call it from init(), it works.  I can call it more than once from init.  BUT, I'll bet when I call it twice from init(), HotSpot is optimizing out the repeated calls.

When you refresh the whole page it comes back.

Oh well.  Give it a shot tomarrow.

Thanks,

Shawn
0
 
ShawnCurryAuthor Commented:
What does it say??  I made it on 1.5-beta2.  It's not an endorsed version.  I set my compile target to 1.4..  Dang.
0
 
RuadRauFlessaCommented:
I use JDK1.5 and it doesn't wanna load the applet at all

-- Opera Java Console --

Java vendor: Sun Microsystems Inc.
Java version: 1.5.0-beta

type 'h' for help

--
java.lang.ClassNotFoundException: test
      at java.net.URLClassLoader+1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at com.opera.PluginPanel.run(PluginPanel.java:424)
      at java.lang.Thread.run(Unknown Source)
0
 
RuadRauFlessaCommented:
Sorry that was a browser problem

Hmmm see what you mean
0
 
ShawnCurryAuthor Commented:
Thanks.  There were actually two problems:  I did need the validate() call, and, I needed to setRootPaneCheckingEnabled(false).  I cracked open the source for JApplet, and it turns out setRootPane() (what I tried first) actually handles this automatically.  The method additionally handles removing the existing rootPane, if there is one.

So, I changed back to setRootPane(), and call validate() right after.  Turns out the invalidate() call is unneccessary, as this happens automatically in Container.remove().

So anyway it works now.  Check it out:

http://www.meta-solutions.com/jfcml/demo/index.html

JFCML so closely models the JComponent heirarchy you can probably figure it out from the simple example provided in the demo.  Otherwise, the javadocs and tutorials are a click away from the demo page! </cheap plug>

Thanks a mil,
Shawn
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.