Example usage for javax.swing.event PopupMenuListener PopupMenuListener

List of usage examples for javax.swing.event PopupMenuListener PopupMenuListener

Introduction

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

Prototype

PopupMenuListener

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {
    JComboBox c = new JComboBox();
    c.addPopupMenuListener(new PopupMenuListener() {

        @Override//from   w w  w .  j  a v a  2 s.co  m
        public void popupMenuCanceled(PopupMenuEvent e) {
            System.out.println(e.getSource());
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            System.out.println(e.getSource());
        }

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            System.out.println(e.getSource());
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().setLayout(new FlowLayout());
    f.getContentPane().add(c);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Popup Menu Listener");
    Container contentPane = frame.getContentPane();

    final String flavors[] = { "Item 1", "Item 2", "Item 3" };

    PopupMenuListener listener = new PopupMenuListener() {
        boolean initialized = false;

        public void popupMenuCanceled(PopupMenuEvent e) {
        }/*  w  ww. j  av a2s  .  co  m*/

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            if (!initialized) {
                JComboBox combo = (JComboBox) e.getSource();
                ComboBoxModel model = new DefaultComboBoxModel(flavors);
                combo.setModel(model);
                initialized = true;
            }
        }
    };

    JComboBox jc = new JComboBox();
    jc.addPopupMenuListener(listener);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    contentPane.add(jc, BorderLayout.NORTH);

    frame.pack();
    frame.setVisible(true);
}

From source file:PopupTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Popup Menu Listener");
    Container contentPane = frame.getContentPane();

    final String flavors[] = { "Item 1", "Item 2", "Item 3" };

    PopupMenuListener listener = new PopupMenuListener() {
        boolean initialized = false;

        public void popupMenuCanceled(PopupMenuEvent e) {
        }//from w  w  w . j  a va  2  s .  c om

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            if (!initialized) {
                JComboBox combo = (JComboBox) e.getSource();
                ComboBoxModel model = new DefaultComboBoxModel(flavors);
                combo.setModel(model);
                initialized = true;
            }
        }
    };

    JComboBox jc = new JComboBox();
    jc.addPopupMenuListener(listener);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    contentPane.add(jc, BorderLayout.NORTH);

    frame.pack();
    frame.show();
}

From source file:PopupSample.java

public static void main(String args[]) {
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Selected: " + actionEvent.getActionCommand());
        }//from  www .j  av  a  2s.c om
    };

    PopupMenuListener popupMenuListener = new 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");
        }
    };

    JFrame frame = new JFrame("Popup Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPopupMenu popupMenu = new JPopupMenu();
    popupMenu.addPopupMenuListener(popupMenuListener);

    JMenuItem cutMenuItem = new JMenuItem("Cut");
    cutMenuItem.addActionListener(actionListener);
    popupMenu.add(cutMenuItem);

    JMenuItem copyMenuItem = new JMenuItem("Copy");
    copyMenuItem.addActionListener(actionListener);
    popupMenu.add(copyMenuItem);

    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.addActionListener(actionListener);
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);

    popupMenu.addSeparator();

    JMenuItem findMenuItem = new JMenuItem("Find");
    findMenuItem.addActionListener(actionListener);
    popupMenu.add(findMenuItem);

    MouseListener mouseListener = new JPopupMenuShower(popupMenu);
    frame.addMouseListener(mouseListener);

    frame.setSize(350, 250);
    frame.setVisible(true);
}

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   ww  w .j av  a2s  . 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:Main.java

public Main() {
    String[] items = { "Item1", "Item2", "Item3", "Item4", "Item5" };
    JComboBox<String> comboBox = new JComboBox<>(items);
    add(comboBox);/*from   w ww. jav  a 2s .c  om*/

    comboBox.addPopupMenuListener(new PopupMenuListener() {
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            JComboBox<String> comboBox = (JComboBox<String>) e.getSource();
            BasicComboPopup popup = (BasicComboPopup) comboBox.getAccessibleContext().getAccessibleChild(0);
            JList list = popup.getList();
            list.setSelectedIndex(2);
        }

        public void popupMenuCanceled(PopupMenuEvent e) {
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

    });
}

From source file:Main.java

public Main() {
    rOne.addActionListener(new ROneAction());
    rTwo.addActionListener(new RTwoAction());
    group = new ButtonGroup();
    group.add(rOne);/*from   w w w.j ava  2s .  c  o  m*/
    group.add(rTwo);

    combo = new JComboBox();
    combo.addItem("No Values");
    combo.addPopupMenuListener(new PopupMenuListener() {
        public void popupMenuCanceled(PopupMenuEvent evt) {
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent evt) {
            jComboBox1PopupMenuWillBecomeInvisible(evt);
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent evt) {
        }
    });

    label = new JLabel("labelLabel");

    this.setLayout(new FlowLayout());
    this.add(rOne);
    this.add(rTwo);
    this.add(combo);
    this.add(label);
    this.pack();
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(400, 300);//w  ww  .j  a va2  s . com
    jPopupMenu1.add(jMenuItem1);
    jTabbedPane1.addTab(null, jLabel1);
    jTabbedPane1.addTab(null, jLabel2);
    getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
    int tabCount = jTabbedPane1.getTabCount();
    for (int i = 0; i < tabCount; i++) {
        JLabel jLabel = new JLabel("Testing the tab" + (i + 1));
        jTabbedPane1.setTabComponentAt(i, jLabel);
        jLabel.setName(String.valueOf(i));
        jLabel.setComponentPopupMenu(jPopupMenu1);
    }
    jPopupMenu1.addPopupMenuListener(new PopupMenuListener() {

        @Override
        public void popupMenuCanceled(final PopupMenuEvent evt) {
        }

        @Override
        public void popupMenuWillBecomeInvisible(final PopupMenuEvent evt) {
        }

        @Override
        public void popupMenuWillBecomeVisible(final PopupMenuEvent evt) {
            JPopupMenu source = (JPopupMenu) evt.getSource();
            JLabel invoker = (JLabel) source.getInvoker();
            JLabel component = (JLabel) jTabbedPane1.getComponentAt(Integer.parseInt(invoker.getName()));
            jMenuItem1.setText(invoker.getText() + ":  " + component.getText());
        }
    });
}

From source file:gdt.jgui.entity.JEntityStructurePanel.java

/**
 * The default constructor./*from  w w w. ja v a2s. c  om*/
 */
public JEntityStructurePanel() {
    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    scrollPane = new JScrollPane();
    add(scrollPane);
    popup = new JPopupMenu();
    popup.addPopupMenuListener(new PopupMenuListener() {
        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            popup.removeAll();
            JMenuItem facetsItem = new JMenuItem("Facets");
            popup.add(facetsItem);
            facetsItem.setHorizontalTextPosition(JMenuItem.RIGHT);
            facetsItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //   System.out.println("EntityStructurePanel:renderer:component locator$="+nodeLocator$);
                    JEntityFacetPanel efp = new JEntityFacetPanel();
                    String efpLocator$ = efp.getLocator();
                    Properties locator = Locator.toProperties(selection$);
                    String entihome$ = locator.getProperty(Entigrator.ENTIHOME);
                    String entityKey$ = locator.getProperty(EntityHandler.ENTITY_KEY);
                    efpLocator$ = Locator.append(efpLocator$, Entigrator.ENTIHOME, entihome$);
                    efpLocator$ = Locator.append(efpLocator$, EntityHandler.ENTITY_KEY, entityKey$);
                    JConsoleHandler.execute(console, efpLocator$);
                }
            });
            JMenuItem copyItem = new JMenuItem("Copy");
            popup.add(copyItem);
            copyItem.setHorizontalTextPosition(JMenuItem.RIGHT);
            copyItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    console.clipboard.clear();
                    String locator$ = (String) node.getUserObject();
                    if (locator$ != null)
                        console.clipboard.putString(locator$);
                }
            });

            if (!isFirst) {
                popup.addSeparator();
                JMenuItem excludeItem = new JMenuItem("Exclude");
                popup.add(excludeItem);
                excludeItem.setHorizontalTextPosition(JMenuItem.RIGHT);
                excludeItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int response = JOptionPane.showConfirmDialog(console.getContentPanel(), "Exclude ?",
                                "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        if (response == JOptionPane.YES_OPTION) {
                            try {
                                Properties locator = Locator.toProperties(selection$);
                                String entihome$ = locator.getProperty(Entigrator.ENTIHOME);
                                String entityKey$ = locator.getProperty(EntityHandler.ENTITY_KEY);
                                Entigrator entigrator = console.getEntigrator(entihome$);
                                Sack component = entigrator.getEntityAtKey(entityKey$);
                                String[] sa = entigrator.ent_listContainers(component);
                                if (sa != null) {
                                    Sack container;
                                    for (String aSa : sa) {
                                        container = entigrator.getEntityAtKey(aSa);
                                        if (container != null)
                                            entigrator.col_breakRelation(container, component);
                                    }
                                }
                                JConsoleHandler.execute(console, JEntityStructurePanel.this.locator$);
                            } catch (Exception ee) {
                                Logger.getLogger(JEntityStructurePanel.class.getName()).info(ee.toString());
                            }
                        }
                    }
                });

                JMenuItem deleteItem = new JMenuItem("Delete");
                popup.add(deleteItem);
                deleteItem.setHorizontalTextPosition(JMenuItem.RIGHT);
                deleteItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int response = JOptionPane.showConfirmDialog(console.getContentPanel(), "Delete ?",
                                "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        if (response == JOptionPane.YES_OPTION) {
                            try {
                                Properties locator = Locator.toProperties(selection$);
                                String entihome$ = locator.getProperty(Entigrator.ENTIHOME);
                                String entityKey$ = locator.getProperty(EntityHandler.ENTITY_KEY);
                                Entigrator entigrator = console.getEntigrator(entihome$);
                                Sack component = entigrator.getEntityAtKey(entityKey$);
                                entigrator.deleteEntity(component);
                                JConsoleHandler.execute(console, JEntityStructurePanel.this.locator$);
                            } catch (Exception ee) {
                                Logger.getLogger(JEntityStructurePanel.class.getName()).info(ee.toString());
                            }
                        }
                    }
                });
            }
            if (hasToInclude()) {
                popup.addSeparator();
                JMenuItem includeItem = new JMenuItem("Include");
                popup.add(includeItem);
                includeItem.setHorizontalTextPosition(JMenuItem.RIGHT);
                includeItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        include();
                        JConsoleHandler.execute(console, JEntityStructurePanel.this.locator$);
                    }
                });
            }
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

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

From source file:com.haulmont.cuba.desktop.gui.components.DesktopLookupField.java

public DesktopLookupField() {
    composition = new JPanel();
    composition.setLayout(new BorderLayout());
    composition.setFocusable(false);//  w w w. j  a  v a  2 s. c o  m

    comboBox = new ExtendedComboBox() {
        @Override
        public void flushValue() {
            super.flushValue();

            flushSelectedValue();
        }
    };
    comboBox.setEditable(true);
    comboBox.setPrototypeDisplayValue("AAAAAAAAAAAA");
    autoComplete = AutoCompleteSupport.install(comboBox, items);

    for (int i = 0; i < comboBox.getComponentCount(); i++) {
        java.awt.Component component = comboBox.getComponent(i);
        component.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                initOptions();

                // update text representation based on entity properties
                updateTextRepresentation();
            }

            @Override
            public void focusLost(FocusEvent e) {
                // Reset invalid value
                checkSelectedValue();
            }
        });
    }
    // set value only on PopupMenu closing to avoid firing listeners on keyboard navigation
    comboBox.addPopupMenuListener(new PopupMenuListener() {
        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            comboBox.updatePopupWidth();
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            if (!autoComplete.isEditableState()) {
                // Only if really item changed
                Object selectedItem = comboBox.getSelectedItem();
                if (selectedItem instanceof ValueWrapper) {
                    Object selectedValue = ((ValueWrapper) selectedItem).getValue();
                    setValue(selectedValue);
                } else if (selectedItem instanceof String && newOptionAllowed && newOptionHandler != null) {
                    restorePreviousItemText();
                    newOptionHandler.addNewOption((String) selectedItem);
                } else if ((selectedItem != null) && !newOptionAllowed) {
                    updateComponent(prevValue);
                }

                updateMissingValueState();

                fireUserSelectionListeners();
            }
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
        }
    });
    comboBox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (settingValue || disableActionListener)
                return;
            Object selectedItem = comboBox.getSelectedItem();
            if (selectedItem instanceof String && newOptionAllowed && newOptionHandler != null) {
                restorePreviousItemText();
                newOptionHandler.addNewOption((String) selectedItem);
            }

            updateMissingValueState();
        }
    });

    setFilterMode(DEFAULT_FILTER_MODE);

    textField = new JTextField();
    textField.setEditable(false);
    UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
    valueFormatter = new DefaultValueFormatter(sessionSource.getLocale());

    composition.add(comboBox, BorderLayout.CENTER);
    impl = comboBox;

    DesktopComponentsHelper.adjustSize(comboBox);
    DesktopComponentsHelper.adjustSize(textField);

    textField.setMinimumSize(
            new Dimension(comboBox.getMinimumSize().width, textField.getPreferredSize().height));

    initClearShortcut();
}