Solved

Applet GUI problems..

Posted on 2004-10-06
10
1,525 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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
 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses

623 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