Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Applet dispaly problem

 
Xinyi Zhang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an applet, which runs well in JBuilder - embeded in a JSP pages that runs on Tomcat. I copy and paste the URL to IE browser, it sometimes display the applet, most of times not. What can this problem is?

Thanks
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the Java console on the browser. On the IE menu, select "Tools", then select "Sun Java Console" from the drop down menu.
 
Xinyi Zhang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess this is a thread issue. See this simple JApplet

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.SwingUtilities;

public class SimpleJApplet extends JApplet {

public void init() {
try {
GUIThread guiThread = new GUIThread();
SwingUtilities.invokeLater(guiThread);
} catch (Exception e) {
e.printStackTrace();
}
}

public void createGUI() {
getContentPane().add(new JButton("Button Two"));
}

class GUIThread implements Runnable {
public void run() {
createGUI();
}
}

}

It displays the GUI in the browser after first loaded, when I refresh the page. Sometimes it displays sometimes not.

import javax.swing.JApplet;
import javax.swing.JButton;


public class SimpleJApplet2 extends JApplet {

public void init() {
getContentPane().add(new JButton("Button One"));
}

}


But using event dispatching thread is recommended. What is wrong with the first piece of code?
[ November 19, 2004: Message edited by: Xinyi Zhang ]
 
Steve Guo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if it is so much trouble to display an applet, then microsoft has really done something to the java movement.

I could display applet before, but not now. Under tools | internet options | advanced, for Microsoft VM, the checkbox for 'java console enabled' is unchecked, I could not check it. I am using win2000. Is this an admin issue?

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually what you're doing is not really recommended. Building the GUI directly in the init() method ensures that the components will be assembled before the applet is painted. Building it in the Swing thread means that the components won't be placed until the applet has already been physically painted on the screen, blank. To make the components show up at that point, you'd need to explicitly validate the applet. You could add a call to validate() at the end of your run() method, and that would clear up the problem, or you could simply move the body of run() into init() and get rid of the Runnable altogether, as in your second example.

You are indeed supposed to use the event thread to make changes to the GUI after it's been physically displayed -- but then still, you have to call validate() to get the layout manager to force your changes into effect. So for example, if you had a separate animation thread, and it made changes to the GUI, then it should use the invokeLater() technique.
 
Xinyi Zhang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Ernest. Your post is very helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic