4

I have a GUI with a JMenuBar, the problem is that sometimes a JMenu doesn't appear until i resize or minimize the JFrame. Can someone help me?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    Help use help you by providing us with a [SSCCE](http://sscce.org/) – MadProgrammer Sep 22 '12 at 21:00
  • 1
    We cannot help you without code that reproduces your problem. Please keep it as simple as possible. – Code-Apprentice Sep 22 '12 at 21:12
  • 3
    Check your [*Initial Threads*](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) usage, and make `setVisible()` _last_. – trashgod Sep 22 '12 at 21:20
  • 1
    A workaround is to recall `setSize()` as the last method for GUI rendering; the problem is as @trashgod mentioned. – Random42 Sep 22 '12 at 21:32
  • 1
    @m3th0dman A future bug is to try to solve this by using.. *"recall setSize() as the last method for GUI rendering;"* – Andrew Thompson Sep 22 '12 at 23:48
  • @m3th0dman I think Andrew means its no use to fight a *bug* with a method that may cause a future *bug* as its not considered good practice to call `setSize()` on `JFrame` rather use `LayoutManager`s and call `pack()` – David Kroukamp Sep 28 '12 at 20:32

2 Answers2

5

I had the same problem, and I solved it by setting the JFrame visible AFTER the code for the menu. So you first create a JFrame after that add the JMenuBar and at last: frame.setVisible(true); (my frame is just called "frame" replace the "frame" with the name of your frame)

Simulant
  • 19,190
  • 8
  • 63
  • 98
Marko
  • 61
  • 4
2

Another, less pretty than @Marko's solution, is to call frame.revalidate() after the frame.setJMenuBar()-call.

Zar
  • 6,786
  • 8
  • 54
  • 76
  • This ends up being better when adding more menu items later on during run-time. Toggling setVisible(...) causes the window to flicker every time something is changed. – user515655 Dec 09 '15 at 06:41