• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GUI doesn't display -- sometimes

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic