List of usage examples for javax.swing JPanel setComponentPopupMenu
@BeanProperty(preferred = true, description = "Popup to show") public void setComponentPopupMenu(JPopupMenu popup)
JPopupMenu
for this JComponent
. From source file:Main.java
public Main() { 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);// w ww .j a v a2 s. c o m 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); }
From source file:ContextMenu.java
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);//from ww w. ja v a 2 s . co m 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); }
From source file:MenuTest.java
public MenuFrame() { setTitle("MenuTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); JMenu fileMenu = new JMenu("File"); fileMenu.add(new TestAction("New")); // demonstrate accelerators JMenuItem openItem = fileMenu.add(new TestAction("Open")); openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O")); fileMenu.addSeparator();//from w ww. j a v a 2s .co m saveAction = new TestAction("Save"); JMenuItem saveItem = fileMenu.add(saveAction); saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S")); saveAsAction = new TestAction("Save As"); fileMenu.add(saveAsAction); fileMenu.addSeparator(); fileMenu.add(new AbstractAction("Exit") { public void actionPerformed(ActionEvent event) { System.exit(0); } }); // demonstrate check box and radio button menus readonlyItem = new JCheckBoxMenuItem("Read-only"); readonlyItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { boolean saveOk = !readonlyItem.isSelected(); saveAction.setEnabled(saveOk); saveAsAction.setEnabled(saveOk); } }); ButtonGroup group = new ButtonGroup(); JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert"); insertItem.setSelected(true); JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype"); group.add(insertItem); group.add(overtypeItem); // demonstrate icons Action cutAction = new TestAction("Cut"); cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif")); Action copyAction = new TestAction("Copy"); copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif")); Action pasteAction = new TestAction("Paste"); pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif")); JMenu editMenu = new JMenu("Edit"); editMenu.add(cutAction); editMenu.add(copyAction); editMenu.add(pasteAction); // demonstrate nested menus JMenu optionMenu = new JMenu("Options"); optionMenu.add(readonlyItem); optionMenu.addSeparator(); optionMenu.add(insertItem); optionMenu.add(overtypeItem); editMenu.addSeparator(); editMenu.add(optionMenu); // demonstrate mnemonics JMenu helpMenu = new JMenu("Help"); helpMenu.setMnemonic('H'); JMenuItem indexItem = new JMenuItem("Index"); indexItem.setMnemonic('I'); helpMenu.add(indexItem); // you can also add the mnemonic key to an action Action aboutAction = new TestAction("About"); aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A')); helpMenu.add(aboutAction); // add all top-level menus to menu bar JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(helpMenu); // demonstrate pop-ups popup = new JPopupMenu(); popup.add(cutAction); popup.add(copyAction); popup.add(pasteAction); JPanel panel = new JPanel(); panel.setComponentPopupMenu(popup); add(panel); // the following line is a workaround for bug 4966109 panel.addMouseListener(new MouseAdapter() { }); }
From source file:org.rdv.datapanel.AbstractDataPanel.java
/** * Get a component for displaying the title in top bar. This implementation * includes a button to remove a specific channel. * /* w w w. ja v a 2 s.co m*/ * Subclasses should overide this method if they don't want the default * implementation. * * @return A component for the top bar * @since 1.3 */ JComponent getTitleComponent() { JPanel titleBar = new JPanel(); titleBar.setOpaque(false); titleBar.setLayout(new BorderLayout()); JPopupMenu popupMenu = new ScrollablePopupMenu(); final String title; if (description != null) { title = "Edit description"; } else { title = "Add description"; } JMenuItem addDescriptionMenuItem = new JMenuItem(title); addDescriptionMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { String response = (String) JOptionPane.showInputDialog(null, "Enter a description", title, JOptionPane.QUESTION_MESSAGE, null, null, description); if (response == null) { return; } else if (response.length() == 0) { setDescription(null); } else { setDescription(response); } } }); popupMenu.add(addDescriptionMenuItem); if (description != null) { JMenuItem removeDescriptionMenuItem = new JMenuItem("Remove description"); removeDescriptionMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { setDescription(null); } }); popupMenu.add(removeDescriptionMenuItem); } popupMenu.addSeparator(); final JCheckBoxMenuItem showChannelsInTitleMenuItem = new JCheckBoxMenuItem("Show channels in title", showChannelsInTitle); showChannelsInTitleMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { setShowChannelsInTitle(showChannelsInTitleMenuItem.isSelected()); } }); popupMenu.add(showChannelsInTitleMenuItem); if (channels.size() > 0) { popupMenu.addSeparator(); Iterator<String> i = channels.iterator(); while (i.hasNext()) { final String channelName = i.next(); JMenuItem unsubscribeChannelMenuItem = new JMenuItem("Unsubscribe from " + channelName); unsubscribeChannelMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { removeChannel(channelName); } }); popupMenu.add(unsubscribeChannelMenuItem); } } // set component popup and mouselistener to trigger it titleBar.setComponentPopupMenu(popupMenu); titleBar.addMouseListener(new MouseInputAdapter() { }); if (description != null) { titleBar.add(getDescriptionComponent(), BorderLayout.WEST); } JComponent titleComponent = getChannelComponent(); if (titleComponent != null) { titleBar.add(titleComponent, BorderLayout.CENTER); } return titleBar; }