Link to home
Start Free TrialLog in
Avatar of ShawnCurry
ShawnCurry

asked on

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 );
      }
      
}
Avatar of Giant2
Giant2

>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?
Avatar of ShawnCurry

ASKER

Nope - check it out:

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

Open your Java console.
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 );
      }
      
}
Mmm. I see an error on my console (1.4.2_05) about the use some swing component.
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
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.
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)
Sorry that was a browser problem

Hmmm see what you mean
ASKER CERTIFIED SOLUTION
Avatar of RuadRauFlessa
RuadRauFlessa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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