Working With Menus and Pop-up Menus - Java Swing

Java examples for Swing:JPopupMenu

Description

Working With Menus and Pop-up Menus

Demo Code

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

public class Main extends JFrame {
  JLabel msgLabel = new JLabel("Right click to see popup menu");
  JTextArea msgText = new JTextArea(10, 60);
  JPopupMenu popupMenu = new JPopupMenu();

  public Main(String title) {
    super(title);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = this.getContentPane();

    contentPane.add(new JScrollPane(msgText), BorderLayout.CENTER);
    contentPane.add(msgLabel, BorderLayout.SOUTH);

    JMenuBar menuBar = getCustomMenuBar();
    this.setJMenuBar(menuBar);

    createPopupMenu();//from   w ww . ja v a2  s.  co m
  }

  private JMenuBar getCustomMenuBar() {
    JMenuBar menuBar = new JMenuBar();

    JMenu fileMenu = getFileMenu();
    JMenu helpMenu = getHelpMenu();

    menuBar.add(fileMenu);
    menuBar.add(helpMenu);

    return menuBar;
  }

  private JMenu getFileMenu() {
    JMenu fileMenu = new JMenu("File");

    fileMenu.setMnemonic(KeyEvent.VK_F);

    JMenu newMenu = getNewMenu();
    fileMenu.add(newMenu);

    JMenuItem openMenuItem = new JMenuItem("Open", KeyEvent.VK_O);
    JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_E);

    fileMenu.add(openMenuItem);

    fileMenu.addSeparator();
    fileMenu.add(exitMenuItem);

    exitMenuItem.addActionListener(e -> System.exit(0));

    return fileMenu;
  }

  private JMenu getNewMenu() {
    JMenu newMenu = new JMenu("New");

    JMenuItem policyMenuItem = new JMenuItem("Policy", KeyEvent.VK_P);
    JMenuItem claimMenuItem = new JMenuItem("Claim", KeyEvent.VK_C);
    newMenu.add(policyMenuItem);
    newMenu.add(claimMenuItem);

    return newMenu;
  }

  private JMenu getHelpMenu() {
    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic(KeyEvent.VK_H);

    JMenuItem indexMenuItem = new JMenuItem("Index", KeyEvent.VK_I);
    JMenuItem aboutMenuItem = new JMenuItem("About", KeyEvent.VK_A);

    KeyStroke f1Key = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
    indexMenuItem.setAccelerator(f1Key);

    helpMenu.add(indexMenuItem);
    helpMenu.addSeparator();
    helpMenu.add(aboutMenuItem);

    indexMenuItem.addActionListener(e -> msgText
        .append("Help >>Index menu item.\n"));

    return helpMenu;
  }

  private void createPopupMenu() {
    JMenuItem popup1 = new JMenuItem("Popup1");
    JMenuItem popup2 = new JMenuItem("Popup2");
    JMenuItem popup3 = new JMenuItem("Popup3");

    ActionListener al = e -> {
      JMenuItem menuItem = (JMenuItem) e.getSource();
      String menuText = menuItem.getText();
      String msg = menuText + " menu item.\n";
      msgText.append(msg);
    };

    popup1.addActionListener(al);
    popup2.addActionListener(al);
    popup3.addActionListener(al);

    popupMenu.add(popup1);
    popupMenu.add(popup2);
    popupMenu.add(popup3);

    MouseListener ml = new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent e) {
        displayPopupMenu(e);
      }

      @Override
      public void mouseReleased(MouseEvent e) {
        displayPopupMenu(e);
      }
    };

    msgText.addMouseListener(ml);
    msgLabel.addMouseListener(ml);
  }

  private void displayPopupMenu(MouseEvent e) {
    if (e.isPopupTrigger()) {
      this.popupMenu.show(e.getComponent(), e.getX(), e.getY());
    }
  }

  public static void main(String[] args) {
    Main frame = new Main("JMenu and JPopupMenu Test");
    frame.pack();
    frame.setVisible(true);
  }
}

Related Tutorials