In Java purposes, a menu is a Java Swing element that enables builders to arrange gadgets in a container. Menus are positioned on a menu bar and you may have a couple of menu merchandise in your menu bar. These menu gadgets act the identical method they do in desktop purposes and internet sites, providing customers the flexibility to work together with an software by a GUI or graphical person enter.
The JMenu class permits programmers to create menus of their graphical purposes. JMenu inherits from JAbstractButton, which itself inherits from JComponent.
We’ve a fantastic record of the Greatest On-line Programs to Study Java if you’re curious about studying the way to program in a course or classroom setting.
How one can Create a Menu in Java
Step one to including a menu to your Java software program is to create a menu bar, which is able to include the menu(s) you create. To attain this, merely use the JMenuBar class, as proven within the instance Java code beneath:
JMenuBar menuBar = new JMenuBar();
To create a menu, merely create an occasion of JMenu. In its constructor, you’ll be able to present the identify of the menu:
JMenu menu = new JMenu("A JMenu");
Subsequent, that you must add gadgets to your record. The JMenuItem class permits programmers so as to add menu gadgets to a menu in Java:
JMenuItem menuItem = new JMenuItem("someText");
As an alternative of offering textual content to the JMenuItem constructor, you possibly can additionally present a picture icon as an alternative. Aside from JMenuItem, builders can even use the JRadioButtonMenuItem or JCheckBoxMenuItem courses to create menu gadgets. These courses create radio button gadgets and test field gadgets, respectively .
If you wish to add totally different menu choice sorts ( i.e radio buttons, checkboxes, or easy menu lists) in your menu gadgets, you’ll be able to separate these utilizing the addSeparator() technique of JMenu:
menu.addSeparator();
It is usually value noting you can create sub-menus inside your menus. After including your menu gadgets (or sub menus) to your menu, that you must add the menu to the menu bar. Then that you must add your menu bar to your body.
menuBar.add(menu); body.setJMenuBar(menuBar);
Beneath is a completely working code instance that demonstrates the way to add a body, a menu bar, a menu, and menu gadgets in Java utilizing Swing elements:
import javax.swing.*; class Menu{ public static void essential(String args[]){ JFrame body = new JFrame(); JMenuBar menuBar = new JMenuBar(); //create menu bar. JMenu menu = new JMenu("A JMenu"); // create menu JMenuItem menuItem1 = new JMenuItem("textual content"); menu.add(menuItem1); JMenuItem menuItem2 = new JMenuItem("photographs"); menu.add(menuItem2); menu.addSeparator(); // line to separate totally different gadgets sorts JRadioButtonMenuItem menuItem3 = new JRadioButtonMenuItem("information"); menu.add(menuItem3); JRadioButtonMenuItem menuItem4 = new JRadioButtonMenuItem("folders"); menu.add(menuItem4); menuBar.add(menu); body.setJMenuBar(menuBar); body.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); body.setSize(400,400); body.setVisible(true); } }
Operating this code will outcome within the following output:
When a person presses A JMenu on the menu bar above, an inventory of menu gadgets will seem.
Learn: Java Instruments to Improve Productiveness
Occasion Dealing with With Menus in Java
If you’re listening for occasions from a JMenuItem, then you should utilize the identical strategy as when you find yourself listening for occasions from a JButton, as detailed in our programming tutorial: How one can Use Buttons in Java Functions.
Nevertheless, if you’re listening for occasions from a radio button, the strategy is a bit totally different. Programmers want so as to add every radio button merchandise to an occasion of ButtonGroup. This ensures that your software permits just one choice for associated radio button gadgets. If they don’t seem to be added to a ButtonGroup, then a person can choose a number of gadgets (which defeats the aim of utilizing radio buttons).
See the code instance beneath displaying how an inventory of fruits is added to a button group:
JRadioButtonMenuItem apple = new JRadioButtonMenuItem("apple"); JRadioButtonMenuItem berry = new JRadioButtonMenuItem("berry"); JRadioButtonMenuItem carrot = new JRadioButtonMenuItem("carrot"); ButtonGroup group = new ButtonGroup(); group.add(apple); group.add(berry); group.add(carrot); apple.addActionListener(this); berry.addActionListener(this); carrot.addActionListener(this);
If you’re utilizing a JCheckBoxMenuItem, then that you must ensure that your occasion dealing with class implements ItemListener. In your occasion handler, that you must implement the itemStateChanged(ItemEvent e) technique:
class Menu implements ItemListener{ public static void essential(String args[]){ } public void itemStateChanged(ItemEvent e) { } }
See the instance Java code beneath, which prints a message on the terminal each time a checkbox menu merchandise is chosen or deselected:
import javax.swing.*; import java.awt.*; import java.awt.occasion.*; class Menu implements ItemListener{ JCheckBoxMenuItem apple; JCheckBoxMenuItem berry; JCheckBoxMenuItem carrot; Menu(){ JFrame body = new JFrame(); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("A JMenu"); apple = new JCheckBoxMenuItem("apple"); berry = new JCheckBoxMenuItem("berry"); carrot = new JCheckBoxMenuItem("carrot"); apple.addItemListener(this); berry.addItemListener(this); carrot.addItemListener(this); menu.add(apple); menu.add(berry); menu.add(carrot); menuBar.add(menu); body.setJMenuBar(menuBar); body.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); body.setSize(400,400); body.setVisible(true); } public static void essential(String args[]){ Menu menuGUI = new Menu(); } public void itemStateChanged(ItemEvent e) { Object supply = e.getItemSelectable(); if (e.getStateChange() == ItemEvent.SELECTED) { if (supply == apple) { System.out.println("Apple Chosen"); } else if (supply == berry) { System.out.println("Berry Chosen"); } else if (supply == carrot) { System.out.println("Carrot Chosen"); } } else { if (supply == apple) { System.out.println("Apple DE-Chosen"); } else if (supply == berry) { System.out.println("Berry DE-Chosen"); } else if (supply == carrot) { System.out.println("Carrot DE-Chosen"); } } } }
Last Ideas on Menus in Java
On this programming tutorial, you might have realized the way to create varied menu gadgets and the way to hear for occasions from menus. Bear in mind to import each the java.awt.* and java.awt.occasion.* modules when utilizing occasion listeners in your menus/Swing purposes.
Learn extra Java programming tutorials and software program improvement ideas.