JComboBox: addPopupMenuListener(PopupMenuListener l) : JComboBox « javax.swing « Java by API






JComboBox: addPopupMenuListener(PopupMenuListener l)

  
import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class Main {
  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) {
      }

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

   
    
  








Related examples in the same category

1.implements JComboBox.KeySelectionManager
2.new JComboBox()
3.new JComboBox(ComboBoxModel aModel)
4.new JComboBox(Vector items)
5.JComboBox: addActionListener(ActionListener lis)
6.JComboBox: addItem(Object o)
7.JComboBox: addItemListener(ItemListener lis)
8.JComboBox: getActionMap()
9.JComboBox: getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
10.JComboBox: getItemAt(int index)
11.JComboBox: getItemCount()
12.JComboBox: getKeySelectionManager()
13.JComboBox: getSelectedIndex()
14.JComboBox: getSelectedItem()
15.JComboBox: insertItemAt(Object anObject, int index)
16.JComboBox: isPopupVisible()
17.JComboBox: removeAllItems()
18.JComboBox: removeItemAt(int anIndex)
19.JComboBox: setEditor(ComboBoxEditor anEditor)
20.JComboBox: setKeySelectionManager(KeySelectionManager aManager)
21.JComboBox: setRenderer(ListCellRenderer aRenderer)
22.JComboBox: setEditable(boolean b)
23.JComboBox: setMaximumRowCount(int count)
24.JComboBox: setSelectedIndex(int anIndex)
25.JComboBox: setSelectedItem(Object anObject)
26.JComboBox: setUI(ComboBoxUI ui)