Solved

Applet GUI problems..

Posted on 2004-10-06
10
1,517 Views
Last Modified: 2013-11-23
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 );
      }
      
}
0
Comment
Question by:ShawnCurry
  • 5
  • 3
  • 2
10 Comments
 
LVL 12

Expert Comment

by:Giant2
ID: 12245879
>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
 
LVL 3

Author Comment

by:ShawnCurry
ID: 12245913
Nope - check it out:

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

Open your Java console.
0
 
LVL 3

Author Comment

by:ShawnCurry
ID: 12245929
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12245934
Mmm. I see an error on my console (1.4.2_05) about the use some swing component.
0
 
LVL 3

Author Comment

by:ShawnCurry
ID: 12245955
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:ShawnCurry
ID: 12245985
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
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12247039
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
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12247045
Sorry that was a browser problem

Hmmm see what you mean
0
 
LVL 3

Accepted Solution

by:
RuadRauFlessa earned 200 total points
ID: 12247125
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
 
LVL 3

Author Comment

by:ShawnCurry
ID: 12255565
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 47
pairs challenge 5 44
Eclipse Neon start with Admin account only 6 47
JList custom Cell Renderer refresh 15 43
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now