Example usage for javax.swing JComboBox addPopupMenuListener

List of usage examples for javax.swing JComboBox addPopupMenuListener

Introduction

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

Prototype

public void addPopupMenuListener(PopupMenuListener l) 

Source Link

Document

Adds a PopupMenu listener which will listen to notification messages from the popup portion of the combo box.

Usage

From source file:Main.java

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

        @Override//from   w  ww .jav a2  s  .  c  o  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: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   www.  j ava2  s. c  o 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.show();
}

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  w w . j  a v  a 2s  . c  o 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:Main.java

public static void main(String[] argv) throws Exception {
    String[] items = { "item1", "item2" };
    JComboBox cb = new JComboBox(items);
    cb.setEditable(true);//from   ww  w .  jav a2s. c  o  m

    MyPopupMenuListener actionListener = new MyPopupMenuListener();
    cb.addPopupMenuListener(actionListener);
}

From source file:Main.java

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

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

    });
}