JMenuBar, JMenu, and JMenuItem Component in Java

This is a tutorial in which we will going to create a program that has the JMenuBar, JMenu, and JMenuItem Component using Java. A menu is a component that provides a way to let the user choose one of several options that can hold other components as choices such as combobox, radio buttons, spinners, and tool bars, and lists. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of jTabbedPaneComponent.java. 2. Import the javax.swing package to access the JFrame,JButton, JLabel,JTextField, and the JTabbedPane class.
  1. import javax.swing.*; // used to access JFrame, JMenu, JMenuBar, and JMenuItem class
3. Initialize your variable in your Main, variable frame for creating JFrame, variable menuBar for JMenuBar, variable fileMenu and transMenu for JMenu class.
  1. JFrame frame = new JFrame("JMenu and JMenuItem Component");
  2. JMenuBar menuBar = new JMenuBar();
  3. JMenu fileMenu = new JMenu("File");
  4. JMenu transMenu = new JMenu("Transaction");
The JMenuBar class here provides the component of the JMenu bar and the JMenu is the menu container of an item to be placed on the menu. To simply add a menu for transactions to the menu bar, we will use the add method of the JMenuBar. We will have two menus on the menu named File and Transaction. Have this code below:
  1. menuBar.add(fileMenu);
  2. menuBar.add(transMenu);
4. Now, we will create a JMenuItem class to create items in the menu bar. We will have two menus for the File and Transaction Menu. For the File Menu, we will create New and Save items. In the Transaction Menu, we will create Copy and Paste items. Have his code below:
  1. JMenuItem newMenuItem = new JMenuItem("New");
  2. JMenuItem saveMenuItem = new JMenuItem("Save");
  3.  
  4. JMenuItem copyItem = new JMenuItem("Copy");
  5. JMenuItem pasteItem = new JMenuItem("Paste");
To add items on the File and Transaction menu, we will use add method of the JMenu class and put inside the variables for JMenuItem. We will also use addSeparator method of the JMenuItem class to have a separator line to the items in the menu bar.
  1. transMenu.add(copyItem);
  2. fileMenu.addSeparator();
  3. transMenu.add(pasteItem);
5. Now, to add the JMenuBar to the frame we will not use the getContentPane.add method but instead we will use the setJMenuBar of the frame.
  1. frame.setJMenuBar(menuBar);
6. Lastly set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.setSize(350, 250);
  2. frame.setVisible(true);
  3. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; // used to access JFrame, JMenu, JMenuBar, and JMenuItem class
  2.  
  3.  
  4. public class jMenuComponent {
  5.  
  6. public static void main(final String args[]) {
  7. JFrame frame = new JFrame("JMenu and JMenuItem Component");
  8. JMenuBar menuBar = new JMenuBar();
  9. JMenu fileMenu = new JMenu("File");
  10. JMenu transMenu = new JMenu("Transaction");
  11.  
  12.  
  13. menuBar.add(fileMenu);
  14. menuBar.add(transMenu);
  15.  
  16. JMenuItem newMenuItem = new JMenuItem("New");
  17. JMenuItem saveMenuItem = new JMenuItem("Save");
  18. fileMenu.add(newMenuItem);
  19. fileMenu.addSeparator();
  20. fileMenu.add(saveMenuItem);
  21.  
  22.  
  23.  
  24.  
  25.  
  26. JMenuItem copyItem = new JMenuItem("Copy");
  27. JMenuItem pasteItem = new JMenuItem("Paste");
  28.  
  29.  
  30. transMenu.add(copyItem);
  31. fileMenu.addSeparator();
  32. transMenu.add(pasteItem);
  33.  
  34. frame.setJMenuBar(menuBar);
  35. frame.setSize(350, 250);
  36. frame.setVisible(true);
  37. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38. }
  39. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment