2

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!

Richard
  • 1,912
  • 20
  • 28
  • 5
    Please try to post a [SSCCE](http://pscode.org/sscce.html). – Howard Jul 13 '11 at 18:47
  • Just out of interest, can somebody clarify why everybody seems to call `validate()` in their Swing examples? I have a whole Swing app I wrote that doesn't call it once... is it necessary or bad/good design practice? – aardvarkk Jul 13 '11 at 18:47
  • 3
    @aardvarkk - I would say in most cases, there is no need to do this. If you are using a layout manager, it should automatically handle most cases. Generally, the proliferation of examples that do include it are (IMHO) symptoms of not understanding how the swing paint system really works. – wolfcastle Jul 13 '11 at 18:52
  • @aardvarkk, It should only be invoked whenever the contents of a visible container are being modified (i.e. removed/added). – mre Jul 13 '11 at 18:52
  • @aardvarkk, In some cases, actually you need validate. For example, you changed size of a frame, and called repaint(), it does not take action immediately, therefore you need a revalidate() call to take action immediately. – Mustafa Zengin Jul 13 '11 at 18:53
  • @Howard: I'm afraid my system is too gigantic to post a compilable example, though I know that dramatically hurts my chances of finding help online. I was hoping someone out there knew some kind of "rule of thumb" for these situations, or some method I need to be calling, or something to that effect. I can tell you that this application has multiple jpanels, splitframes, tabs, etc., if that helps at all. – Richard Jul 13 '11 at 20:59
  • `When it doesn't the entire program freezes and I have to kill it.` that's indicated another issue, memory lack 1) did you create `JFrames` or `JDialog` on fly, everytimes a new one 2) did you create lots of `JPanels` with a new `JComponents`, everytime a new 3) or that came from JSR296 and you didn't check Netbeans BugParade – mKorbel Jul 13 '11 at 21:18

1 Answers1

4

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    Awesome-- thank you very much. In my code, I'm not doing anything in the event dispatch thread. I will make the appropriate changes, and post back if I still have problems. – Richard Jul 14 '11 at 12:01
  • It might be a little early to tell, but it looks like that fixed it. Thanks again! Specifically, I find using the invokeAndWait() method works best for me. – Richard Jul 19 '11 at 00:33
  • For reference, this [Q&A](http://stackoverflow.com/questions/7787998/how-to-generate-exceptions-from-repaintmanager/7788806#7788806) referenced a related article that proved particularly useful. – trashgod Oct 17 '11 at 17:33