Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI doesn't display -- sometimes

 
Stephen Tracey
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Rea
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
JFrame.setDefaultLookAndFeelDecorated(true);

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

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

//Display the window.
frame.pack();
frame.setVisible(true);
}

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() {
createAndShowGUI();
}
});
}

Hope it helps...
 
Stephen Tracey
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephen Tracey
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic