1. Introduction – JMenuBar, JMenu, JMenuItem
The components JMenuBar, JMenu and JMenuItem all together form the Java Swing Menu based user interface. Java Swing Top-Level containers houses the JMenuBar. The JMenuBar control can hold one or more JMenu control. One or more JMenuItem can be present in a single JMenu. To better understand this relation, have a look at the below picture:
The above picture shows a JFrame with a title. It contains a menu bar with two menus called File and Edit. The menu-bar shows only the title of the menus. When the user clicks the menu title, Java Swing will open the popup menu. The File Popup Menu contains 4 menu items and Edit Popup Menu contains three menu items. Each MenuItem will produce an ActionEvent which one can handle to take required action. For Example, when the user clicks File->New, the action event handler will have the code to open a new file.
2. About Java Swing JMenu Example
The below screenshot shows the example we will create in the following sections:
Our example has a JMenuBar with two JMenu in it. The menus are File and Edit. File Menu has 4 MenuItems which include a JSeparator. This separator is separating Open, Save menu items with the Exit menu items. When you click Open MenuItem under the File Menu, a dialog will be displayed. This helps us to learn how to respond to the Menu Item click event. The Edit menu has 3 Menu Items. Now let us code this example.
3. Create JMenuBar
After setting the Size and position of the JFrame window, we create a JMenuBar at line number 13 and then set it to the JFrame. The method setJMenuBar accepts JMenuBar instance and set it to the JFrame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@SuppressWarnings("serial") public class JMenuSimple extends JFrame { public JMenuSimple(String title) { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 02: Create Menu Bar and add to JFrame JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); |
4. Create JMenu
Next, we must create two Menus File and Edit. We use JMenu constructor to create them and the constructor takes a string which will be the Title for the Menu. After creating the JMenu, we add them to the menu-bar via its add method.
1 2 3 4 5 |
//Sample 03: Create Menus & add to Menubar JMenu mnuFile = new JMenu("File"); JMenu mnuEdit = new JMenu("Edit"); menubar.add(mnuFile); menubar.add(mnuEdit); |
5. Create JMenuItem
Now we have Menu-Bar and Menu ready. We know that a JMenu can hold one or more menu items. In the below code, we create six JMenuItem objects and in the constructor; we pass the display string for the menu items. We also construct the JSeparator at line number 5 and separate the exit menu with the Open & Save Menu items. In code snippet 4a, we add Open, Save and Exit MenuItems along with Separator to the File menu. After this, we added the remaining JMenuItem objects to the Edit menu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Sample 04: Create Menu Items //4a: Menu Items for File JMenuItem miFileOpen = new JMenuItem("Open"); JMenuItem miFileSave = new JMenuItem("Save"); JSeparator sep1 = new JSeparator(); JMenuItem miFileExit = new JMenuItem("Exit"); mnuFile.add(miFileOpen); mnuFile.add(miFileSave); mnuFile.add(sep1); mnuFile.add(miFileExit); //4b: Menu Items for Edit JMenuItem miEditCut = new JMenuItem("Cut"); JMenuItem miEditCopy = new JMenuItem("Copy"); JMenuItem miEditPaste = new JMenuItem("Paste"); mnuEdit.add(miEditCut); mnuEdit.add(miEditCopy); mnuEdit.add(miEditPaste); |
6. Handle JMenuItem Click
JMenuItem raises ActionEvent when we click it. One can handle this event via ActionListener just like how we handle the button click event. In the below code, we write an anonymous inner handler actionPerformed to handle the ActionEvent. Inside the handler, we just display a message box to state the event is handled. Here, we handled the event for File->Open MenuItem only. The procedure is the same for other Menu Items as well.
1 2 3 4 5 6 7 8 |
//Sample 05: Handle Clicked Menu Items miFileOpen.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( JMenuSimple.this, "Opening the File..."); } }); |
7. YouTube Demo – Code Implementation
8. Code Reference
8.1 MainEntry.java
1 2 3 4 5 6 7 8 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { JMenuSimple frame = new JMenuSimple("Simple Swing Menu"); frame.setVisible(true); } } |
8.2 JMenuSimple.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
@SuppressWarnings("serial") public class JMenuSimple extends JFrame { public JMenuSimple(String title) { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 02: Create Menu Bar and add to JFrame JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); //Sample 03: Create Menus & add to Menubar JMenu mnuFile = new JMenu("File"); JMenu mnuEdit = new JMenu("Edit"); menubar.add(mnuFile); menubar.add(mnuEdit); //Sample 04: Create Menu Items //4a: Menu Items for File JMenuItem miFileOpen = new JMenuItem("Open"); JMenuItem miFileSave = new JMenuItem("Save"); JSeparator sep1 = new JSeparator(); JMenuItem miFileExit = new JMenuItem("Exit"); mnuFile.add(miFileOpen); mnuFile.add(miFileSave); mnuFile.add(sep1); mnuFile.add(miFileExit); //4b: Menu Items for Edit JMenuItem miEditCut = new JMenuItem("Cut"); JMenuItem miEditCopy = new JMenuItem("Copy"); JMenuItem miEditPaste = new JMenuItem("Paste"); mnuEdit.add(miEditCut); mnuEdit.add(miEditCopy); mnuEdit.add(miEditPaste); //Sample 05: Handle Clicked Menu Items miFileOpen.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( JMenuSimple.this, "Opening the File..."); } }); } } |
Categories: Swing
Tags: ActionEvent, ActionListener, JMenu, JMenuBar, JMenuItem, JSeparator, setJMenuBar