Richard
Richard

Reputation: 2100

Java swing JMenuBar sometimes doesn't show up

I am working on a Java desktop application, and sometimes my jmenubar shows up, sometimes it doesn't. When it doesn't the entire program freezes and I have to kill it.

I am creating it and setting it in my constructor for the frame.

The code:


    // Load any settings we might have made from last time.
    loadSettings();

    menuBar = createMenuBar();
    setJMenuBar(menuBar);    

    // re-evaluate and re-layout things, since they've been changed.
    validate();


    addWindowListener(this);    

As you can see in my code, I added a validate() call, hoping that would help. It does, actually, but the problem still happens from time to time.

Thanks in advance for your help!

Upvotes: 2

Views: 1199

Answers (1)

trashgod
trashgod

Reputation: 205775

The essential rules are these:

  1. Verify that all GUI elements are constructed on the event dispatch thread.

  2. Verify that no exceptions are swallowed, especially on the event dispatch thread.

Note that the EDT will restart itself after an exception is thrown. Depending on the circumstances, the application may appear to freeze. Although you should see something on the console, the article Uncaught Exception Handlers may offer some insight.

Upvotes: 4

Related Questions