Java/Swing JFC/Popup menu

Материал из Java эксперт
Версия от 06:39, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A popup menu is sometimes called a context menu

 

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class PopupMenu {
  public static void main(String[] args) {
    JFrame frame = new JFrame("JPopupMenu");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Toolkit toolkit = frame.getToolkit();
    final JPopupMenu menu = new JPopupMenu();
    JMenuItem menuItemBeep = new JMenuItem("Beep");
    menuItemBeep.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        toolkit.beep();
      }
    });
    menu.add(menuItemBeep);
    JMenuItem menuItemClose = new JMenuItem("Close");
    menuItemClose.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
    menu.add(menuItemClose);
    frame.addMouseListener(new MouseAdapter() {
      public void mouseReleased(MouseEvent e) {
        if (e.getButton() == e.BUTTON3) {
          menu.show(e.getComponent(), e.getX(), e.getY());
        }
      }
    });
    frame.setSize(250, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





Configure all popup menus to be heavyweight

 
import javax.swing.JPopupMenu;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean lwPopup = JPopupMenu.getDefaultLightWeightPopupEnabled(); // true
    // Globally use heavyweight components for all popup menus
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
  }
}





Creating a Popup Menu

 
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class Main {
  public static void main(String[] argv) throws Exception {
    final JPopupMenu menu = new JPopupMenu();
    JMenuItem item = new JMenuItem("Item Label");
//  item.addActionListener(actionListener);
    menu.add(item);
    JButton component = new JButton("button");
    component.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent evt) {
        if (evt.isPopupTrigger()) {
          menu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
      }
      public void mouseReleased(MouseEvent evt) {
        if (evt.isPopupTrigger()) {
          menu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
      }
    });
  }
}





Creating a Popup Menu with Nested Menus

 
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
public class Main {
  public static void main(String[] argv) throws Exception {
    final JPopupMenu popupMenu = new JPopupMenu();
    JMenu submenu = new JMenu("SubMenu1");
    submenu.add("asdf");
    submenu.add("asdf");
    // Add submenu to popup menu
    popupMenu.add(submenu);
  }
}





Force the popup menu of a JMenu to be heavyweight

 
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class Main {
  public static void main(String[] argv) throws Exception {
    JMenu menu = new JMenu("Menu Label");
    menu.add(new JMenuItem("Item Label"));
    boolean lwPopup = menu.getPopupMenu().isLightWeightPopupEnabled(); // true
    menu.getPopupMenu().setLightWeightPopupEnabled(false);
  }
}





Forcing a Popup Menu to Be a Heavyweight Component

 
import javax.swing.JPopupMenu;
public class Main {
  public static void main(String[] argv) throws Exception {
    JPopupMenu popupMenu = new JPopupMenu();
    boolean lwPopup = popupMenu.isLightWeightPopupEnabled(); // true
    popupMenu.setLightWeightPopupEnabled(false);
  }
}





JPopupMenu with mouse event

 
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class PopUpColorMenu implements ActionListener {
  Component selectedComponent;
  public PopUpColorMenu() {
    JFrame frame = new JFrame();
    final JPopupMenu colorMenu = new JPopupMenu("Color");
    colorMenu.add(makeMenuItem("Red"));
    colorMenu.add(makeMenuItem("Green"));
    colorMenu.add(makeMenuItem("Blue"));
    MouseListener mouseListener = new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        checkPopup(e);
      }
      public void mouseClicked(MouseEvent e) {
        checkPopup(e);
      }
      public void mouseReleased(MouseEvent e) {
        checkPopup(e);
      }
      private void checkPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
          selectedComponent = e.getComponent();
          colorMenu.show(e.getComponent(), e.getX(), e.getY());
        }
      }
    };
    frame.setLayout(new FlowLayout());
    JButton button = new JButton("Uno");
    button.addMouseListener(mouseListener);
    frame.add(button);
    button = new JButton("Due");
    button.addMouseListener(mouseListener);
    frame.add(button);
    button = new JButton("Tre");
    button.addMouseListener(mouseListener);
    frame.add(button);
    frame.getContentPane().addMouseListener(mouseListener);
    frame.setSize(200, 50);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    String color = e.getActionCommand();
    if (color.equals("Red"))
      selectedComponent.setBackground(Color.red);
    else if (color.equals("Green"))
      selectedComponent.setBackground(Color.green);
    else if (color.equals("Blue"))
      selectedComponent.setBackground(Color.blue);
  }
  private JMenuItem makeMenuItem(String label) {
    JMenuItem item = new JMenuItem(label);
    item.addActionListener(this);
    return item;
  }
  public static void main(String[] args) {
    new PopUpColorMenu();
  }
}





set Component Popup Menu

 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ContextMenu implements ActionListener {
  JTextArea textArea = new JTextArea();
  public ContextMenu() {
    final JPopupMenu contextMenu = new JPopupMenu("Edit");
    contextMenu.add(makeMenuItem("Save"));
    contextMenu.add(makeMenuItem("Save As"));
    contextMenu.add(makeMenuItem("Close"));
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    frame.add(panel);
    panel.setComponentPopupMenu(contextMenu);
    textArea.setInheritsPopupMenu(true);
    panel.add(BorderLayout.CENTER, textArea);
    JTextField textField = new JTextField();
    textField.setInheritsPopupMenu(true);
    panel.add(BorderLayout.SOUTH, textField);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 200);
    frame.setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    textArea.append(e.getActionCommand() + "\n");
  }
  private JMenuItem makeMenuItem(String label) {
    JMenuItem item = new JMenuItem(label);
    item.addActionListener(this);
    return item;
  }
  public static void main(String[] args) {
    new ContextMenu();
  }
}





setInheritsPopupMenu

 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ContextMenu implements ActionListener {
  JTextArea textArea = new JTextArea();
  public ContextMenu() {
    final JPopupMenu contextMenu = new JPopupMenu("Edit");
    contextMenu.add(makeMenuItem("Save"));
    contextMenu.add(makeMenuItem("Save As"));
    contextMenu.add(makeMenuItem("Close"));
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    frame.add(panel);
    panel.setComponentPopupMenu(contextMenu);
    textArea.setInheritsPopupMenu(true);
    panel.add(BorderLayout.CENTER, textArea);
    JTextField textField = new JTextField();
    textField.setInheritsPopupMenu(true);
    panel.add(BorderLayout.SOUTH, textField);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 200);
    frame.setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    textArea.append(e.getActionCommand() + "\n");
  }
  private JMenuItem makeMenuItem(String label) {
    JMenuItem item = new JMenuItem(label);
    item.addActionListener(this);
    return item;
  }
  public static void main(String[] args) {
    new ContextMenu();
  }
}