Java Swing How to - Add Empty JMenuItems in JMenuBar on JFrame








Question

We would like to know how to add Empty JMenuItems in JMenuBar on JFrame.

Answer

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
//from  ww w. java  2s  . c o m
public class Main extends JFrame {
  static JMenuBar exampleMenuBar = new JMenuBar();
  static JMenu fileMenu = new JMenu("Help");
  static JMenuItem Exit = new JMenuItem("Exit");

  public static void main(String args[]) {
    new Main().setVisible(true);
  }

  public Main() {
    setSize(100, 100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setJMenuBar(exampleMenuBar);
    exampleMenuBar.add(fileMenu);
    fileMenu.add(Exit);
    Exit.addActionListener(e -> System.exit(0));
  }
}