The right way to Use Menus in Java


In Java functions, a menu is a Java Swing element that permits builders to prepare objects in a container. Menus are positioned on a menu bar and you’ll have multiple menu merchandise in your menu bar. These menu objects act the identical manner they do in desktop functions and websites, providing customers the power to work together with an software by means of a GUI or graphical consumer enter.

The JMenu class permits programmers to create menus of their graphical functions. JMenu inherits from JAbstractButton, which itself inherits from JComponent.

We’ve got an incredible record of the Greatest On-line Programs to Be taught Java in case you are keen on studying program in a course or classroom setting.

The right way to 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 realize 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, you might want to add objects to your record. The JMenuItem class permits programmers so as to add menu objects to a menu in Java:

JMenuItem menuItem = new JMenuItem("someText"); 

As an alternative of offering textual content to the JMenuItem constructor, you may additionally present a picture icon as an alternative. Other than JMenuItem, builders also can use the JRadioButtonMenuItem or JCheckBoxMenuItem courses to create menu objects. These courses create radio button objects and verify field objects, respectively .

If you wish to add completely different menu choice varieties ( i.e radio buttons, checkboxes, or easy menu lists) to your menu objects, 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 objects (or sub menus) to your menu, you might want to add the menu to the menu bar. Then you might want to add your menu bar to your body.

menuBar.add(menu);           
body.setJMenuBar(menuBar);

Beneath is a totally working code instance that demonstrates add a body, a menu bar, a menu, and menu objects in Java utilizing Swing parts:

import javax.swing.*;
class Menu{
 
   public static void principal(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("photos");
      menu.add(menuItem2);
 
      menu.addSeparator(); // line to separate completely different objects varieties
      JRadioButtonMenuItem menuItem3 = new JRadioButtonMenuItem("recordsdata");
      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 end result within the following output:

Java Swing Menu Tutorial

When a consumer presses A JMenu on the menu bar above, an inventory of menu objects will seem.

Learn: Java Instruments to Enhance Productiveness

Occasion Dealing with With Menus in Java

In case you are listening for occasions from a JMenuItem, then you should use the identical strategy as when you’re listening for occasions from a JButton, as detailed in our programming tutorial: The right way to Use Buttons in Java Functions.

Nonetheless, in case you are listening for occasions from a radio button, the strategy is a bit completely 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 objects. If they don’t seem to be added to a ButtonGroup, then a consumer can choose a number of objects (which defeats the aim of utilizing radio buttons).

See the code instance beneath exhibiting 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);

In case you are utilizing a JCheckBoxMenuItem, then you might want to ensure that your occasion dealing with class implements ItemListener. In your occasion handler, you might want to implement the itemStateChanged(ItemEvent e) technique:

class Menu implements ItemListener{

public static void principal(String args[]){
 }

public void itemStateChanged(ItemEvent e) {

}
}

See the instance Java code beneath, which prints a message on the terminal every 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 principal(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’ve got realized create varied menu objects and 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 functions.

Learn extra Java programming tutorials and software program improvement suggestions.



Source_link

Leave a Reply

Your email address will not be published.