Class JMenuBar for creating dropdown menu in Java - ProgrammerTech
Language:

Search

Class JMenuBar for creating dropdown menu in Java

  • Share this:
Class JMenuBar for creating dropdown menu in Java

Class JMenuBar to create a dropdown menu

The class ( JMenuBar ) is used to create a dropdown menu in a window. In this lesson, we will learn how to create a menu. In many programs, we note that it is necessary to use the drop-down menus to navigate the project sections or to navigate the forms through the drop-down menus.

 

The classes used in the menu bar are the Java language

  1. JMenuBar.
  2. Jmenu.
  3. JmenuItem.
  4. JCheckBoxMenuItem.
  5. JRadioButtonMenuItem.

 

Packages and classes that can be called in JMenuBar

import javax.swing.JFrame;   /windows creation package
import javax.swing.JMenu;   /Special package to write the name of the list
import javax.swing.JMenuBar;   /The menu bar
import javax.swing.JMenuItem;   /Items that appear in the list

We will explain these packages step by step for easy understanding.

Example 1

package foreachexample;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class ForEachExample{
     public static void main(String []args){
        JFrame f=new JFrame();
        JMenu menu=new JMenu("file");
        JMenuBar mb=new JMenuBar();
        mb.add(menu);
        f.setJMenuBar(mb);
        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);
     }
}

At the beginning of the code, we called several packages that we need in this lesson, which is the JFrame package, and we called the package. For writing the name of the menu JMenu, the JMenuBar package, and the package of items that will appear in the menu, which is JMenuItem. Then we entered the main program. We created the interface name class. Then we created the list via the JMenu class and put the word "file" to appear in this list. Then we created the menu bar class via JMenuBar and added the menu to the bar via the mb.add(menu) command. Then we add the bar to the interface via setJMenuBar and set the length and width of the interface and fit the interface with the added elements and finally show the interface and the elements.

When this code is executed, we will get the following result.

1-en-2.webp

We notice that we have created the tape, which is a 3D tape with the word "File" inside it. So far we haven't created the dropdown list, we just created the name of the list. We created the menu itself and the name of the menu, but the dropdown items are not created yet. We will create dropdown items with the JMenuItem package, we will now create them with the following example.

Example 2

package foreachexample;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class ForEachExample{
     public static void main(String []args){
        JFrame f=new JFrame();
        JMenu menu=new JMenu("file");
        JMenuItem l1,l2;
        JMenuBar mb=new JMenuBar();
        l1=new JMenuItem("new");
        l2=new JMenuItem("open");
        menu.add(l1);
        menu.add(l2);
        mb.add(menu);
        f.setJMenuBar(mb);
        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);
     }
}

At the beginning of the code, we called several packages that we need in this program, which is the JFrame package, and we called the package. For writing the name of the menu JMenu and the menu bar package JMenuBar and the package of items that will appear in the menu is JMenuItem. Then we entered the main program. We created the interface name class, then we created the menu via the JMenu class, and we put the word "file" to appear in this menu. And we created the number of objects for the list we added two elements to it via JMenuItem , they are l1 and l2 . Then we created the menu bar class Via JMenuBar and then we have added two items in the dropdown menu which are "Save" and "New" and we have added the items to the menu. We add the menu to the bar via the mb.add(menu) command and then add the bar to the interface via setJMenuBar. And we set the length and width of the interface and we fit the interface with the added elements and finally we showed the interface and the elements.

And when this code is executed, we will find the result.

2-en-3.webp

Example 3

package maaaai;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Maaaai {
    public static void main(String[] args) {
        JFrame frame = new JFrame("interface lists");
        frame.setSize(640, 380);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        
        JMenu file = new JMenu("file");
        JMenu edit = new JMenu("Edit");
        JMenu help = new JMenu("help");
        
        menuBar.add(file);
        menuBar.add(edit);
        menuBar.add(help);
        
        frame.setJMenuBar(menuBar);
        frame.setVisible(true);
    }
}

At the beginning of the code, we called several packages that we need in this program, which is the JFrame package, and we called the package. For writing the name of the menu JMenu and the menu bar package JMenuBar and the menu package which is JMenu. Then we entered the main program. We created the interface name class, then we created the menu via the JMenu class, and we put the word "file" to appear in this menu and the word Edit and Help. Then we added these menus to the interface via add and showed the interface.

3-en-1.webp

Example 4

package maaaai;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Maaaai {
    public static void main(String[] args) {
        JFrame frame = new JFrame("lists");
        frame.setSize(500, 250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("list");
        JMenu subMenu = new JMenu("Click for more");
        JMenuItem Alsadig = new JMenuItem("الصادق");
        JMenuItem Muhammad = new JMenuItem("محمـد");
        JMenuItem Ahmad = new JMenuItem("احمد");
        JMenuItem Saja = new JMenuItem("سجى");
        JMenuItem Doha = new JMenuItem("ضحى");
        JMenuItem Wa3ad = new JMenuItem("وعد");
        menuBar.add(menu);
        menu.add(Alsadig);
        menu.add(Muhammad);
        menu.add(Ahmad);
        menu.add(subMenu);
        subMenu.add(Saja);
        subMenu.add(Doha);
        subMenu.add(Wa3ad);
        frame.setJMenuBar(menuBar);
        frame.setVisible(true);
    }  
}

At the beginning of the code, we called several packages that we need in this program, which is the JFrame package, and we called the package. For writing the name of the menu JMenu and the menu bar package JMenuBar and the package of items that will appear in the menu is JMenuItem. Then we entered the main program. We created the interface name class, then we created the menu via the JMenu class, and we put the word "menus" to appear in this menu. Then we added the data for the main and child menus in the program for this interface.

4-en-2.webp

Do not postpone the implementation of duties, because their accumulation leads you to prepare them in a hurry without checking


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com