List of usage examples for javax.swing JPopupMenu JPopupMenu
public JPopupMenu()
JPopupMenu
without an "invoker". From source file:MediumPopupMenuSample.java
public static void main(String args[]) { // Define ActionListener ActionListener aListener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("Selected: " + event.getActionCommand()); }/*from w ww . j a v a 2 s .c o m*/ }; // Define PopupMenuListener PopupMenuListener pListener = new PopupMenuListener() { public void popupMenuCanceled(PopupMenuEvent event) { System.out.println("Canceled"); } public void popupMenuWillBecomeInvisible(PopupMenuEvent event) { System.out.println("Becoming Invisible"); } public void popupMenuWillBecomeVisible(PopupMenuEvent event) { System.out.println("Becoming Visible"); } }; // Define JFrame frame = new JFrame("Popup Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPopupMenu.setDefaultLightWeightPopupEnabled(false); // Create popup menu, attach popup menu listener final JPopupMenu popupMenu = new JPopupMenu(); popupMenu.addPopupMenuListener(pListener); // Cut JMenuItem cutItem = new JMenuItem("Cut"); cutItem.addActionListener(aListener); popupMenu.add(cutItem); // Copy JMenuItem copyItem = new JMenuItem("Copy"); copyItem.addActionListener(aListener); popupMenu.add(copyItem); // Paste JMenuItem pasteItem = new JMenuItem("Paste"); pasteItem.addActionListener(aListener); pasteItem.setEnabled(false); popupMenu.add(pasteItem); // Separator popupMenu.addSeparator(); // Find JMenuItem findItem = new JMenuItem("Find"); findItem.addActionListener(aListener); popupMenu.add(findItem); // Enable showing MouseListener mouseListener = new JPopupMenuShower(popupMenu); frame.addMouseListener(mouseListener); Button button = new Button("Label"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(popupMenu.isLightWeightPopupEnabled()); } }); frame.getContentPane().add(button, BorderLayout.SOUTH); frame.setSize(350, 250); frame.setVisible(true); }
From source file:MenuDemo1.java
public static void main(String[] args) { // Create a window for this demo JFrame frame = new JFrame("Menu Demo"); JPanel panel = new JPanel(); frame.getContentPane().add(panel, "Center"); // Create an action listener for the menu items we will create // The MenuItemActionListener class is defined below ActionListener listener = new MenuItemActionListener(panel); // Create some menu panes, and fill them with menu items // The menuItem() method is important. It is defined below. JMenu file = new JMenu("File"); file.setMnemonic('F'); file.add(menuItem("New", listener, "new", 'N', KeyEvent.VK_N)); file.add(menuItem("Open...", listener, "open", 'O', KeyEvent.VK_O)); file.add(menuItem("Save", listener, "save", 'S', KeyEvent.VK_S)); file.add(menuItem("Save As...", listener, "saveas", 'A', KeyEvent.VK_A)); JMenu edit = new JMenu("Edit"); edit.setMnemonic('E'); edit.add(menuItem("Cut", listener, "cut", 0, KeyEvent.VK_X)); edit.add(menuItem("Copy", listener, "copy", 'C', KeyEvent.VK_C)); edit.add(menuItem("Paste", listener, "paste", 0, KeyEvent.VK_V)); // Create a menubar and add these panes to it. JMenuBar menubar = new JMenuBar(); menubar.add(file);//from ww w. j a v a2s. c o m menubar.add(edit); // Add menubar to the main window. Note special method to add menubars frame.setJMenuBar(menubar); // Now create a popup menu and add the some stuff to it final JPopupMenu popup = new JPopupMenu(); popup.add(menuItem("Open...", listener, "open", 0, 0)); popup.addSeparator(); // Add a separator between items JMenu colors = new JMenu("Colors"); // Create a submenu popup.add(colors); // and add it to the popup menu // Now fill the submenu with mutually-exclusive radio buttons ButtonGroup colorgroup = new ButtonGroup(); colors.add(radioItem("Red", listener, "color(red)", colorgroup)); colors.add(radioItem("Green", listener, "color(green)", colorgroup)); colors.add(radioItem("Blue", listener, "color(blue)", colorgroup)); // Arrange to display the popup menu when the user clicks in the window panel.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { // Check whether this is the right type of event to pop up a popup // menu on this platform. Usually checks for right button down. if (e.isPopupTrigger()) popup.show((Component) e.getSource(), e.getX(), e.getY()); } }); // Finally, make our main window appear frame.setSize(450, 300); frame.setVisible(true); }
From source file:Main.java
public static JPopupMenu createStdEditPopupMenu(final JTextComponent[] fields) { final JPopupMenu popupMenu = new JPopupMenu(); /* text fields popup menu: "Cut" */ final JMenuItem cutMenuItem = new JMenuItem("Cut", 't'); cutMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { final Component c = popupMenu.getInvoker(); if (c instanceof JTextComponent) { ((JTextComponent) c).cut(); }/*from w w w.j a va2 s . c om*/ } }); popupMenu.add(cutMenuItem); /* text fields popup menu: "Copy" */ final JMenuItem copyMenuItem = new JMenuItem("Copy", 'C'); copyMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { final Component c = popupMenu.getInvoker(); if (c instanceof JTextComponent) { ((JTextComponent) c).copy(); } } }); popupMenu.add(copyMenuItem); /* text fields popup menu: "Paste" */ final JMenuItem pasteMenuItem = new JMenuItem("Paste", 'P'); pasteMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { final Component c = popupMenu.getInvoker(); if (c instanceof JTextComponent) { ((JTextComponent) c).paste(); } } }); popupMenu.add(pasteMenuItem); popupMenu.addSeparator(); /* text fields popup menu: "Select All" */ final JMenuItem selectAllMenuItem = new JMenuItem("Select All", 'A'); selectAllMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { final Component c = popupMenu.getInvoker(); if (c instanceof JTextComponent) { ((JTextComponent) c).selectAll(); } } }); popupMenu.add(selectAllMenuItem); /* add mouse listeners to the specified fields */ for (final JTextComponent f : fields) { f.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { processMouseEvent(e); } @Override public void mouseReleased(MouseEvent e) { processMouseEvent(e); } private void processMouseEvent(MouseEvent e) { if (e.isPopupTrigger()) { popupMenu.show(e.getComponent(), e.getX(), e.getY()); popupMenu.setInvoker(f); } } }); } return popupMenu; }
From source file:Main.java
/** * Make a popu menu using lists of actions. Each action list will be separated by * a menu separator. Empty action lists, or null-valued action lists will be ignored as if * the were not there.//from w w w .ja va2 s .c om * @param aActionLists * @return A popup menu. */ public static JPopupMenu popupBuilder(Action[]... aActionLists) { // Prepare the popup. final JPopupMenu lMenu = new JPopupMenu(); // First we are going to filter out the empty lists. final java.util.List<Action[]> lActionList = new ArrayList<Action[]>(); for (Action[] lList : aActionLists) if (lList != null && lList.length > 0) lActionList.add(lList); // Build the popup menu. for (int i = 0; i < lActionList.size(); i++) { for (Action lAction : lActionList.get(i)) lMenu.add(lAction); if (i < lActionList.size() - 1) lMenu.addSeparator(); } return lMenu; }
From source file:MouseTest.java
public MouseTest() { super();/*ww w .j a v a2s .c o m*/ final JPopupMenu pop = new JPopupMenu(); pop.add(new JMenuItem("Cut")); pop.add(new JMenuItem("Copy")); pop.add(new JMenuItem("Paste")); pop.addSeparator(); pop.add(new JMenuItem("Select All")); pop.setInvoker(this); MouseListener popup = new MouseListener() { public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { if (e.isPopupTrigger()) { showPopup(e); } } public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { showPopup(e); } } private void showPopup(MouseEvent e) { pop.show(e.getComponent(), e.getX(), e.getY()); } }; addMouseListener(popup); MouseListener drawing1 = new MouseListener() { public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { color = Color.RED; startX = endX = e.getX(); startY = endY = e.getY(); repaint(); } public void mouseReleased(MouseEvent e) { color = Color.BLACK; repaint(); } }; addMouseListener(drawing1); MouseMotionListener drawing2 = new MouseMotionListener() { public void mouseDragged(MouseEvent e) { endX = e.getX(); endY = e.getY(); repaint(); } public void mouseMoved(MouseEvent e) { } }; addMouseMotionListener(drawing2); }
From source file:Main.java
public Main() { popup = new JPopupMenu(); ActionListener menuListener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed."); }/*w w w.j a va 2 s . co m*/ }; JMenuItem item; popup.add(item = new JMenuItem(new ImageIcon("left.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Center", new ImageIcon("center.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Right", new ImageIcon("right.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Full", new ImageIcon("full.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.addSeparator(); popup.add(item = new JMenuItem("Settings . . .")); item.addActionListener(menuListener); popup.setLabel("Justification"); popup.setBorder(new BevelBorder(BevelBorder.RAISED)); popup.addPopupMenuListener(new PopupPrintListener()); addMouseListener(new MousePopupListener()); }
From source file:Main.java
public Main() { popup = new JPopupMenu(); ActionListener menuListener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed."); }/*from w ww .j a v a2 s. c o m*/ }; JMenuItem item; popup.add(item = new JMenuItem("Left", new ImageIcon("left.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Center", new ImageIcon("center.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Right", new ImageIcon("right.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Full", new ImageIcon("full.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.addSeparator(); popup.add(item = new JMenuItem("Settings . . .")); item.addActionListener(menuListener); popup.setLabel("Justification"); popup.setBorder(new BevelBorder(BevelBorder.RAISED)); popup.addPopupMenuListener(new PopupPrintListener()); addMouseListener(new MousePopupListener()); }
From source file:Main.java
public Main() { popup = new JPopupMenu(); ActionListener menuListener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed."); }/* www . j a v a 2s . c o m*/ }; JMenuItem item; popup.add(item = new JMenuItem("Left")); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Center", new ImageIcon("center.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Right", new ImageIcon("right.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Full", new ImageIcon("full.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.addSeparator(); popup.add(item = new JMenuItem("Settings . . .")); item.addActionListener(menuListener); popup.setLabel("Justification"); popup.setBorder(new BevelBorder(BevelBorder.RAISED)); popup.addPopupMenuListener(new PopupPrintListener()); addMouseListener(new MousePopupListener()); }
From source file:MainClass.java
public MainClass() { popup = new JPopupMenu(); ActionListener menuListener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed."); }//from www .ja v a2 s. c om }; JMenuItem item; popup.add(item = new JMenuItem("Left", new ImageIcon("left.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Center", new ImageIcon("center.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Right", new ImageIcon("right.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Full", new ImageIcon("full.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.addSeparator(); popup.add(item = new JMenuItem("Settings . . .")); item.addActionListener(menuListener); popup.setLabel("Justification"); popup.setBorder(new BevelBorder(BevelBorder.RAISED)); popup.addPopupMenuListener(new PopupPrintListener()); addMouseListener(new MousePopupListener()); }
From source file:MainClass.java
public MainClass() { popup = new JPopupMenu(); slider = new SliderMenuItem(); popup.add(slider);//from w w w .j av a 2s . co m popup.add(new JSeparator()); JMenuItem ticks = new JCheckBoxMenuItem("Slider Tick Marks"); ticks.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { slider.setPaintTicks(!slider.getPaintTicks()); } }); JMenuItem labels = new JCheckBoxMenuItem("Slider Labels"); labels.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { slider.setPaintLabels(!slider.getPaintLabels()); } }); popup.add(ticks); popup.add(labels); }