Reputation: 387
I am having issue with JMenu
popup. It only sometimes works. Sometimes I don't get a java popup at all. Sometimes my File and Edit options are completely missing. This is what my code looks like.
import javax.swing.*;
public class menu {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(400,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu ("File");
mb.add(file);
JMenu edit = new JMenu("Edit");
mb.add(edit);
f.setJMenuBar(mb);
}
}
Upvotes: 0
Views: 57
Reputation: 347194
Call setVisible
on the JFrame
only after you have initialised the core UI...
JFrame f = new JFrame();
// Don't call this here...
//f.setVisible(true);
f.setSize(400,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu ("File");
mb.add(file);
JMenu edit = new JMenu("Edit");
mb.add(edit);
f.setJMenuBar(mb);
// Call it here
f.setVisible(true);
Also, make sure you are creating/updating the UI only from within the context of the Event Dispatching Thread, see Initial Threads for more details
Upvotes: 3