Java Tutorial/Swing/JPopupMenu — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 04:57, 1 июня 2010

Adding action listener to popup menuitem

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
// Define ActionListener
class PopupActionListener implements ActionListener {
  public void actionPerformed(ActionEvent actionEvent) {
    System.out.println("Selected: " + actionEvent.getActionCommand());
  }
}
public class JPopupMenuItemActionListener {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("PopupSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Create popup menu, attach popup menu listener
    JPopupMenu popupMenu = new JPopupMenu("Title");
    ActionListener actionListener = new PopupActionListener();
    // Cut
    JMenuItem cutMenuItem = new JMenuItem("Cut");
    popupMenu.add(cutMenuItem);
    // Copy
    JMenuItem copyMenuItem = new JMenuItem("Copy");
    copyMenuItem.addActionListener(actionListener);
    popupMenu.add(copyMenuItem);
    // Paste
    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.addActionListener(actionListener);
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);
    // Separator
    popupMenu.addSeparator();
    // Find
    JMenuItem findMenuItem = new JMenuItem("Find");
    findMenuItem.addActionListener(actionListener);
    popupMenu.add(findMenuItem);
    JButton label = new JButton();
    frame.add(label);
    label.setComponentPopupMenu(popupMenu);
    frame.setSize(350, 250);
    frame.setVisible(true);
  }
}





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);
  }
}





Customizing JPopupMenu Look and Feel

Property StringObject TypePopupMenu.actionMapActionMapPopupMenu.backgroundColorPopupMenu.borderBorderPopupMenu.consumeEventOnCloseBooleanPopupMenu.fontFontPopupMenu.foregroundColorPopupMenu.popupSoundStringPopupMenu.selectedWindowInputMapBindingsObject[ ]PopupMenu.selectedWindowInputMapBindings.RightToLeftObject[ ]PopupMenuSeparatorUIStringPopupMenuUIString


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: the container for pop-up menu components

Adding Menu Items to a JPopupMenu



public JMenuItem add(JMenuItem menuItem);
public JMenuItem add(String label);
public JMenuItem add(Action action);
public void addSeparator();





To display JPopupMenu on an AWT component

If you need to enable the display of a Panel (an AWT component), you can configure it at the individual JPopupMenu level or for your entire applet or application. At the individual pop-up level, just set the lightWeightPopupEnabled property to false.

The setInheritsPopupMenu() method accepts a boolean argument. When true, and no component pop-up menu has been directly set for the component, the parent container will be explored for a pop-up.

At the system level, this is done as follows:



// From now on, all JPopupMenus will be heavyweight
JPopupMenu.setDefaultLightWeightPopupEnabled(false);





Using PopupMenuListener

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
// Define PopupMenuListener
class MyPopupMenuListener implements PopupMenuListener {
  public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
    System.out.println("Canceled");
  }
  public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {
    System.out.println("Becoming Invisible");
  }
  public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {
    System.out.println("Becoming Visible");
  }
}
public class PopupMenuListenerDemo {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("PopupSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Create popup menu, attach popup menu listener
    JPopupMenu popupMenu = new JPopupMenu("Title");
    PopupMenuListener popupMenuListener = new MyPopupMenuListener();
    popupMenu.addPopupMenuListener(popupMenuListener);
    // Cut
    JMenuItem cutMenuItem = new JMenuItem("Cut");
    popupMenu.add(cutMenuItem);
    // Copy
    JMenuItem copyMenuItem = new JMenuItem("Copy");
    popupMenu.add(copyMenuItem);
    // Paste
    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);
    // Separator
    popupMenu.addSeparator();
    // Find
    JMenuItem findMenuItem = new JMenuItem("Find");
    popupMenu.add(findMenuItem);
    JButton label = new JButton();
    frame.add(label);
    label.setComponentPopupMenu(popupMenu);
    frame.setSize(350, 250);
    frame.setVisible(true);
  }
}