• We just launched and are currently in beta. Join us as we build and grow the community.

JMenuBar, JMenu, and JMenuItem Component in Java

Melvin

Cyber Awareness Educator
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
84
Likes
66
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
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. JMenuItem

    copyItem =

    new

    JMenuItem

    (

    "Copy"

    )

    ;
  4. 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:

jmenu.png


Here's the full code of this tutorial:

  1. import

    javax.swing.*

    ;

    // used to access JFrame, JMenu, JMenuBar, and JMenuItem class


  2. public

    class

    jMenuComponent {

  3. public

    static

    void

    main(

    final

    String

    args[

    ]

    )

    {
  4. JFrame

    frame =

    new

    JFrame

    (

    "JMenu and JMenuItem Component"

    )

    ;
  5. JMenuBar

    menuBar =

    new

    JMenuBar

    (

    )

    ;
  6. JMenu

    fileMenu =

    new

    JMenu

    (

    "File"

    )

    ;
  7. JMenu

    transMenu =

    new

    JMenu

    (

    "Transaction"

    )

    ;


  8. menuBar.add

    (

    fileMenu)

    ;
  9. menuBar.add

    (

    transMenu)

    ;

  10. JMenuItem

    newMenuItem =

    new

    JMenuItem

    (

    "New"

    )

    ;
  11. JMenuItem

    saveMenuItem =

    new

    JMenuItem

    (

    "Save"

    )

    ;
  12. fileMenu.add

    (

    newMenuItem)

    ;
  13. fileMenu.addSeparator

    (

    )

    ;
  14. fileMenu.add

    (

    saveMenuItem)

    ;





  15. JMenuItem

    copyItem =

    new

    JMenuItem

    (

    "Copy"

    )

    ;
  16. JMenuItem

    pasteItem =

    new

    JMenuItem

    (

    "Paste"

    )

    ;


  17. transMenu.add

    (

    copyItem)

    ;
  18. fileMenu.addSeparator

    (

    )

    ;
  19. transMenu.add

    (

    pasteItem)

    ;

  20. frame.setJMenuBar

    (

    menuBar)

    ;
  21. frame.setSize

    (

    350

    , 250

    )

    ;
  22. frame.setVisible

    (

    true

    )

    ;
  23. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  24. }
  25. }

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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

337,656

337,664

Top