File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes GUI doesn't display -- sometimes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "GUI doesn Watch "GUI doesn New topic

GUI doesn't display -- sometimes

Stephen Tracey

Joined: Apr 08, 2004
Posts: 13
I scanned 200 RMI postings today -- such a cliched subject! Here's a fresh new topic, one that may not have come up before.

Sometimes my GUI doesn't display! Usually it does, though. Does anyone know why? Here is the main part of the code. (You'll notice that I do some of the construction elsewhere separately for client and server.)

The interesting part is that on the executions when it doesn't display (maybe 10% of the time), the code gets to the "pack" call 6 lines from the end, BUT IT DOESN'T EXECUTE THE LAST 5 LINES. Plus, this subroutine is called at the end of the constructor, and the last line of the constructor doesn't get executed either. It's almost like an exception is occurring, but Pack doesn't throw!

Thanks in advance for your help.
YungSheng Chang
Ranch Hand

Joined: Jul 28, 2003
Posts: 44
I try to implement your code, but some member variables are not defined, such as gui.

According to my experience, there must some runtime exception thrown. Maybe the exception is swallowed. Make sure you do not catch an exception and print nothing. It is harmful for debug.

One suggestion is pack() may go to the line before setVisibl(true) at the end of gui initiation.

I play Java for fun.<br /> <br />SCJP / SCJD / SCWCD / SCBCD
Martin Rea

Joined: May 04, 2004
Posts: 12
Hi Stephen,
I can't see what the pack() method is? Is this because your GUI extends JFrame or ...

I don't use pack() in my application because it packs the GUI and I don't like to have different sizes depending on what I have started.

I Tried to put in a pack command on my main panel (JFrame) and start it severel times - no problems.

So the easy solution - why not just delete the pack() call?
Yanick Labelle
Ranch Hand

Joined: Dec 29, 2003
Posts: 50
Hi, at Sun, they say that everything that modifies the gui must be done in the event dispatching thread. Even the creation of the gui is done this way. Else, you may have problems with one component referencing another component and that component is not fully generated yet, or things like that... Anyway, that's the way I did it in my certification. Here is an example from sun:

* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
private static void createAndShowGUI() {
//Make sure we have nice window decorations.

//Create and set up the window.
frame = new JFrame("FocusConceptsDemo");

//Create and set up the content pane.
JComponent newContentPane = new FocusConceptsDemo();
newContentPane.setOpaque(true); //content panes must be opaque

//Display the window.

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {

Hope it helps...
Stephen Tracey

Joined: Apr 08, 2004
Posts: 13
Sorry for the delayed response, I didn't get emails when you posted. So I assumed that no one had replied.

YungSheng: There is no exception swallowing happening, at least not by code that I've written! Tell me, why do you suggest moving the pack 4 lines down? I'll give it a try, I'd just like to know why you say that.

Martin: Yes, the application is a JFrame. Pack sets the size for me automatically, otherwise I'd have to pick some numbers and do it manually. It should work with pack! Lastly, remember that it occurs only "occasionally", and then only on my server GUI! (Uh oh, I think that I neglected to mention that before.)

Yanick: Thanks. But have you simplified your main? Otherwise, your whole application is running in the new Runnable thread, right? In that case I don't see how that makes an improvement. Comment?
Andy Zhu
Ranch Hand

Joined: May 26, 2004
Posts: 145
I think the symptom of your codes seems like a threading problem: unpredictable. My guess here is that your program has some threaded programming, which sometimes preempties your "this" gui thread. As it is recommended in java 1.4 for thread safety, the gui staff is betting run in event-dispatching thread. So the person replied above with javax.swing.SwingUtilities.invokeLater() may be the answer.

--------<br />Andy Zhu<br />scjp 1.4<br />scjd 1.4<br />SAS Certified Programmer 9.0
Stephen Tracey

Joined: Apr 08, 2004
Posts: 13
I agree. Here's the link:
subject: GUI doesn't display -- sometimes
It's not a secret anymore!