Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to use Jmenu in Java?

JMenu in Java

Condition for Using Jmenu in Java

  • Description: To use JMenu in Java, the javax.swing.JMenu class is utilized to create menus in a graphical user interface. A JMenuBar is first created, which will hold the menu items. Then, one or more JMenu objects are created to represent individual menus, such as "File", "Edit", or "Help". Each JMenu can contain multiple JMenuItem objects, which represent the actions within that menu, like "Open", "Save", or "Exit". The add() method is used to add the JMenuItem objects to the respective JMenu. The JMenuBar is then set to the frame using frame.setJMenuBar(menuBar) to display the menu bar at the top of the application window. To handle user interactions, ActionListener can be attached to each menu item, triggering specific actions when selected. Menus can also include submenus, separators, and keyboard shortcuts for enhanced functionality.
Sample Source Code
  • # JMenuExample.java
    package JavaSamples2;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;

    public class JMenuExample {
    public static void main(String[] args) {
    JFrame frame = new JFrame("Database Menu Example");
    frame.setLayout(new BorderLayout());

    JLabel label = new JLabel("Select a user from the menu", JLabel.CENTER);
    frame.add(label, BorderLayout.CENTER);

    JMenuBar menuBar = new JMenuBar();

    JMenu userMenu = new JMenu("Users");

    populateMenu(userMenu, label);

    menuBar.add(userMenu);

    frame.setJMenuBar(menuBar);

    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }

    private static void populateMenu(JMenu userMenu, JLabel label) {
    try {
    String url = "jdbc:mysql://localhost:3306/Crud";
    String username = "root";
    String password = "root@123";

    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection(url, username, password);

    String query = "SELECT userid, name, mark FROM crud_table";
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery(query);

    while (rs.next()) {
    String userId = rs.getString("userid");
    String name = rs.getString("name");
    String mark = rs.getString("mark");
    String menuItemText = userId + " - " + name;

    JMenuItem menuItem = new JMenuItem(menuItemText);
    menuItem.addActionListener((ActionEvent e) -> {
    String userDetails = "ID: " + userId + ", Name: " + name + ", Marks: " + mark;

    label.setText(userDetails);
    });
    userMenu.add(menuItem);
    }

    connection.close();
    } catch (ClassNotFoundException | SQLException e) {
    System.out.println(e.getMessage());
    }
    }
    }
Screenshots
  • STEP 1: The user creates the GUI for JMenu.
  • The user creates the GUI for JMenu.

  • STEP 2: When the user selects the "Users" menu, a list of available menu options will be displayed.
  • When the user selects the 'Users' menu, a list of available menu options will be displayed.

  • STEP 3: When the user clicks the second menu item, it will be displayed at the center of the page.
  • When the user clicks the second menu item, it will be displayed at the center of the page.

  • STEP 4: When the user clicks the third menu item, it will be displayed at the center of the page.
  • When the user clicks the third menu item, it will be displayed at the center of the page.