Example usage for javax.swing JPopupMenu JPopupMenu

List of usage examples for javax.swing JPopupMenu JPopupMenu

Introduction

In this page you can find the example usage for javax.swing JPopupMenu JPopupMenu.

Prototype

public JPopupMenu() 

Source Link

Document

Constructs a JPopupMenu without an "invoker".

Usage

From source file:fi.elfcloud.client.tree.ClusterNode.java

@Override
public JPopupMenu popupMenu(BeaverGUI gui) {
    JPopupMenu popup = new JPopupMenu();
    populatePopupMenu(popup, gui);
    return popup;
}

From source file:de.tntinteractive.portalsammler.gui.MainDialog.java

private JPopupMenu createConfigMenu() {
    final JPopupMenu menu = new JPopupMenu();

    final JMenuItem sourceConfig = new JMenuItem("Quellen verwalten...");
    sourceConfig.addActionListener(new ActionListener() {
        @Override/*from w w  w .j  av  a  2 s.c o  m*/
        public void actionPerformed(final ActionEvent e) {
            MainDialog.this.gui.showConfigGui(MainDialog.this.getStore());
        }
    });
    menu.add(sourceConfig);

    final JMenuItem changePassword = new JMenuItem("Neues Passwort...");
    changePassword.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            try {
                MainDialog.this.changePassword();
            } catch (final GeneralSecurityException ex) {
                MainDialog.this.gui.showError(ex);
            } catch (final IOException ex) {
                MainDialog.this.gui.showError(ex);
            }
        }
    });
    menu.add(changePassword);

    return menu;
}

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 .ja  va2 s  . c o 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:CubaHSQLDBServer.java

private void addCopyPopup(final JTextArea source) {
    final JPopupMenu popup = new JPopupMenu();
    popup.add(new AbstractAction("Copy to clipboard") {
        @Override/* ww  w. j a v  a2  s.  c  om*/
        public void actionPerformed(ActionEvent e) {
            StringSelection contents = new StringSelection(source.getText());
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(contents, contents);
        }
    });
    source.add(popup);
    source.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(source, e.getX(), e.getY());
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(source, e.getX(), e.getY());
            }
        }
    });
}

From source file:org.pgptool.gui.ui.mainframe.MainFrameView.java

@Override
protected void internalInitComponents() {
    initMenuBar();
    initFormComponents();
    initToolBar();

    ctxMenu = new JPopupMenu();
}

From source file:de.ailis.xadrian.components.ComplexEditor.java

/**
 * Constructor//from   w  ww.  j  a  v  a  2 s  . c  o m
 *
 * @param complex
 *            The complex to edit
 * @param file
 *            The file from which the complex was loaded. Null if it not
 *            loaded from a file.
 */
public ComplexEditor(final Complex complex, final File file) {
    super();
    setLayout(new BorderLayout());

    this.complex = complex;
    this.file = file;

    // Create the text pane
    this.textPane = new JTextPane();
    this.textPane.setEditable(false);
    this.textPane.setBorder(null);
    this.textPane.setContentType("text/html");
    this.textPane.setDoubleBuffered(true);
    this.textPane.addHyperlinkListener(this);
    this.textPane.addCaretListener(this);

    // Create the popup menu for the text pane
    final JPopupMenu popupMenu = new JPopupMenu();
    popupMenu.add(new CopyAction(this));
    popupMenu.add(new SelectAllAction(this));
    popupMenu.addSeparator();
    popupMenu.add(new AddFactoryAction(this));
    popupMenu.add(new ChangeSectorAction(this.complex, this, "complex"));
    popupMenu.add(new ChangeSunsAction(this));
    popupMenu.add(new ChangePricesAction(this));
    popupMenu.add(new JCheckBoxMenuItem(new ToggleBaseComplexAction(this)));
    SwingUtils.setPopupMenu(this.textPane, popupMenu);

    final HTMLDocument document = (HTMLDocument) this.textPane.getDocument();

    // Set the base URL of the text pane
    document.setBase(Main.class.getResource("templates/"));

    // Modify the body style so it matches the system font
    final Font font = UIManager.getFont("Label.font");
    final String bodyRule = "body { font-family: " + font.getFamily() + "; font-size: " + font.getSize()
            + "pt; }";
    document.getStyleSheet().addRule(bodyRule);

    // Create the scroll pane
    final JScrollPane scrollPane = new JScrollPane(this.textPane);
    add(scrollPane);

    // Redraw the content
    redraw();

    fireComplexState();
}

From source file:cz.muni.fi.javaseminar.kafa.bookregister.gui.MainWindow.java

private void initBooksTable() {
    booksTable.getColumnModel().getColumn(2)
            .setCellEditor(new DatePickerCellEditor(new SimpleDateFormat("dd. MM. yyyy")));
    booksTable.getColumnModel().getColumn(2).setCellRenderer(new DefaultTableCellRenderer() {

        @Override//from www.jav a  2 s . c  o m
        public Component getTableCellRendererComponent(JTable jtable, Object value, boolean selected,
                boolean hasFocus, int row, int column) {

            if (value instanceof Date) {

                // You could use SimpleDateFormatter instead
                value = new SimpleDateFormat("dd. MM. yyyy").format(value);

            }

            return super.getTableCellRendererComponent(jtable, value, selected, hasFocus, row, column);

        }
    });
    booksTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    JPopupMenu booksPopupMenu = new JPopupMenu();
    JMenuItem deleteBook = new JMenuItem("Delete");
    booksPopupMenu.addPopupMenuListener(new PopupMenuListener() {

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    int rowAtPoint = booksTable.rowAtPoint(
                            SwingUtilities.convertPoint(booksPopupMenu, new Point(0, 0), booksTable));
                    if (rowAtPoint > -1) {
                        booksTable.setRowSelectionInterval(rowAtPoint, rowAtPoint);
                    }
                }
            });
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
            // TODO Auto-generated method stub

        }
    });
    deleteBook.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (booksTable.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(MainWindow.this, "You haven't selected any book.");
                return;
            }
            Book book = booksTableModel.getBooks().get(booksTable.getSelectedRow());
            new SwingWorker<Void, Void>() {

                @Override
                protected Void doInBackground() throws Exception {
                    log.debug("Deleting book: " + book.getName() + " from database.");
                    bookManager.deleteBook(book);
                    return null;
                }

                @Override
                protected void done() {
                    try {
                        get();
                    } catch (Exception e) {
                        log.error("There was an exception thrown while deleting a book.", e);
                        return;
                    }

                    updateModel();
                }

            }.execute();

        }
    });
    booksPopupMenu.add(deleteBook);
    booksTable.setComponentPopupMenu(booksPopupMenu);
}

From source file:net.sourceforge.jasa.view.OrderBookView.java

@Override
public void afterPropertiesSet() throws Exception {
    this.setModel(this);
    this.setPreferredSize(new Dimension(400, 200));
    JPopupMenu popup = new JPopupMenu();
    popup.add(new AbstractAction("Inspect order") {

        @Override/*from w  ww .  java  2s .  co  m*/
        public void actionPerformed(ActionEvent e) {
            int row = getSelectedRow();
            int column = getSelectedColumn();
            Order order = null;
            if (column > 1) {
                order = asks.get(row);
            } else {
                order = bids.get(row);
            }
            if (order != null) {
                Inspector.inspect(order);
            }
        }
    });

    setComponentPopupMenu(popup);
}

From source file:coolmap.application.widget.impl.WidgetUserGroup.java

private void initPopup() {
    JPopupMenu popup = new JPopupMenu();
    table.setComponentPopupMenu(popup);//from  w  ww . j  a  v  a2 s  . co m

    JMenuItem item = new JMenuItem("Rename");
    popup.add(item);
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            List<String> groupNames = getSelectedGroups();
            if (groupNames.isEmpty()) {
                return;
            }

            String returnVal = JOptionPane.showInputDialog(CoolMapMaster.getCMainFrame(),
                    "Please provide a new name:");
            if (returnVal == null || returnVal.length() == 0) {
                returnVal = "Untitled";
            }

            int counter = 0;
            String newName;
            for (String groupName : groupNames) {
                if (counter == 0) {
                    newName = returnVal;
                } else {
                    newName = returnVal + "_" + counter;
                }

                //new name must not exist
                int subCounter = 0;
                String name = newName;
                while (nodeGroups.containsKey(name)) {
                    subCounter++;
                    name = newName + "_" + subCounter;
                }
                newName = name;

                Color c = nodeColor.get(groupName);
                Set<VNode> nodes = new HashSet(nodeGroups.get(groupName));

                nodeColor.remove(groupName);
                nodeGroups.removeAll(groupName);

                nodeColor.put(newName, c);
                nodeGroups.putAll(newName, nodes);

                //                    System.out.println(newName + " " + c + " " + nodes);
                counter++;
            }

            updateTable();

        }
    });
    ////////////////////////////////////////////////////////////////////////

    //remove operations
    item = new JMenuItem("Remove");
    popup.add(item);
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            List<String> groupNames = getSelectedGroups();
            for (String group : groupNames) {
                nodeColor.remove(group);
                nodeGroups.removeAll(group);
            }
            updateTable();
        }
    });

    //add separarator
    popup.addSeparator();
    JMenu insertRow = new JMenu("Add selected to row");
    popup.add(insertRow);

    JMenu insertColumn = new JMenu("Add selected to column");
    popup.add(insertColumn);

    item = new JMenuItem("prepend", UI.getImageIcon("prependRow"));
    insertRow.add(item);
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            insertRow(0);
        }
    });

    item = new JMenuItem("prepend", UI.getImageIcon("prependColumn"));
    insertColumn.add(item);
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            insertColumn(0);
        }
    });

    item = new JMenuItem("insert", UI.getImageIcon("insertRow"));
    item.setToolTipText("Insert selected groups to the selected region in the active coolmap view");
    insertRow.add(item);
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            CoolMapObject obj = CoolMapMaster.getActiveCoolMapObject();
            if (obj == null) {
                return;
            }

            int index = 0;
            ArrayList selectedRows = obj.getCoolMapView().getSelectedRows();
            if (!selectedRows.isEmpty()) {
                index = ((Range<Integer>) selectedRows.iterator().next()).lowerEndpoint();
            }
            insertRow(index);
        }
    });

    item = new JMenuItem("insert", UI.getImageIcon("insertColumn"));
    item.setToolTipText("Insert selected groups to the selected region in the active coolmap view");
    insertColumn.add(item);
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            CoolMapObject obj = CoolMapMaster.getActiveCoolMapObject();
            if (obj == null) {
                return;
            }

            int index = 0;
            ArrayList selectedColumns = obj.getCoolMapView().getSelectedColumns();
            if (!selectedColumns.isEmpty()) {
                index = ((Range<Integer>) selectedColumns.iterator().next()).lowerEndpoint();
            }
            insertColumn(index);
        }
    });

    item = new JMenuItem("append", UI.getImageIcon("appendRow"));
    insertRow.add(item);
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (CoolMapMaster.getActiveCoolMapObject() == null) {
                return;
            }
            insertRow(CoolMapMaster.getActiveCoolMapObject().getViewNumRows());
        }
    });

    item = new JMenuItem("append", UI.getImageIcon("appendColumn"));
    insertColumn.add(item);
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (CoolMapMaster.getActiveCoolMapObject() == null) {
                return;
            }
            insertColumn(CoolMapMaster.getActiveCoolMapObject().getViewNumColumns());
        }
    });

    item = new JMenuItem("replace", UI.getImageIcon("replaceRow"));
    insertRow.add(item);
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            replaceRow();
        }
    });

    item = new JMenuItem("replace", UI.getImageIcon("replaceColumn"));
    insertColumn.add(item);
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            replaceColumn();
        }
    });
}

From source file:au.org.ala.delta.editor.ui.image.ImageOverlayEditorController.java

public JPopupMenu buildPopupMenu() {
    disableActions();// w ww  .  j  a  v a 2s.c  o  m
    boolean itemImage = (_selection.getSelectedImage().getSubject() instanceof Item);
    List<String> popupMenuActions = new ArrayList<String>();
    if (_selection.getSelectedOverlay() != null) {
        popupMenuActions.add("editSelectedOverlay");
        popupMenuActions.add("deleteSelectedOverlay");
        popupMenuActions.add("-");
    }
    popupMenuActions.add("deleteAllOverlays");
    popupMenuActions.add("-");
    popupMenuActions.add("displayImageSettings");
    popupMenuActions.add("-");
    popupMenuActions.add("cancelPopup");

    JPopupMenu popup = new JPopupMenu();
    MenuBuilder.buildMenu(popup, popupMenuActions, _actions);

    if (_selection.getSelectedOverlay() != null) {
        List<String> stackOverlayMenuActions = new ArrayList<String>();
        stackOverlayMenuActions.add("stackSelectedOverlayHigher");
        stackOverlayMenuActions.add("stackSelectedOverlayLower");
        stackOverlayMenuActions.add("stackSelectedOverlayOnTop");
        stackOverlayMenuActions.add("stackSelectedOverlayOnBottom");
        JMenu stackOverlayMenu = new JMenu(_resources.getString("overlayPopup.stackOverlayMenu"));
        MenuBuilder.buildMenu(stackOverlayMenu, stackOverlayMenuActions, _actions);
        popup.add(stackOverlayMenu, 2);
    }
    List<String> insertOverlayMenuActions = new ArrayList<String>();
    insertOverlayMenuActions.add("addTextOverlay");
    if (itemImage) {
        insertOverlayMenuActions.add("addItemDescriptionOverlay");
    }
    insertOverlayMenuActions.add("-");
    if (!itemImage) {
        insertOverlayMenuActions.add("addAllUsualOverlays");
        insertOverlayMenuActions.add("addFeatureDescriptionOverlay");
        insertOverlayMenuActions.add("addStateOverlay");
        insertOverlayMenuActions.add("addHotspot");
        insertOverlayMenuActions.add("-");
    }
    insertOverlayMenuActions.add("addOkOverlay");
    insertOverlayMenuActions.add("addCancelOverlay");
    if (!itemImage) {
        insertOverlayMenuActions.add("addNotesOverlay");
    } else {
        insertOverlayMenuActions.add("addImageNotesOverlay");
    }

    JMenu insertOverlayMenu = new JMenu(_resources.getString("overlayPopup.insertOverlayMenu"));
    MenuBuilder.buildMenu(insertOverlayMenu, insertOverlayMenuActions, _actions);
    int indexModifier = _selection.getSelectedOverlay() == null ? 4 : 0;
    popup.add(insertOverlayMenu, 5 - indexModifier);

    List<String> alignButtonsMenuActions = new ArrayList<String>();
    alignButtonsMenuActions.add("useDefaultButtonAlignment");
    alignButtonsMenuActions.add("*alignButtonsVertically");
    alignButtonsMenuActions.add("*alignButtonsHorizontally");
    alignButtonsMenuActions.add("*dontAlignButtons");
    JMenu alignButtonsMenu = new JMenu(_resources.getString("overlayPopup.alignButtonsMenu"));
    alignButtonsMenu.setEnabled(getButtonOverlays().size() > 0);
    JMenuItem[] items = MenuBuilder.buildMenu(alignButtonsMenu, alignButtonsMenuActions, _actions);
    switch (_alignment) {
    case NO_ALIGN:
        items[3].setSelected(true);
        break;
    case ALIGN_HORIZONTALLY:
        items[2].setSelected(true);
        break;
    case ALIGN_VERTICALLY:
        items[1].setSelected(true);
        break;
    }

    popup.add(alignButtonsMenu, 7 - indexModifier);

    return popup;
}