![]() |
The Java Developers Almanac 1.4 |
|
e767. Determining When the Menu of a JComboBox Component Is Displayed // Create component
String[] items = {"item1", "item2"};
JComboBox cb = new JComboBox(items);
cb.setEditable(true);
// Create and register listener
MyPopupMenuListener actionListener = new MyPopupMenuListener();
cb.addPopupMenuListener(actionListener);
class MyPopupMenuListener implements PopupMenuListener {
// This method is called just before the menu becomes visible
public void popupMenuWillBecomeVisible(PopupMenuEvent evt) {
JComboBox cb = (JComboBox)evt.getSource();
}
// This method is called just before the menu becomes hidden
public void popupMenuWillBecomeInvisible(PopupMenuEvent evt) {
JComboBox cb = (JComboBox)evt.getSource();
}
// This method is called when menu is hidden because the user cancelled it
public void popupMenuCanceled(PopupMenuEvent evt) {
JComboBox cb = (JComboBox)evt.getSource();
}
}
e757. Getting and Setting the Selected Item in a JComboBox Component e758. Getting the Items in a JComboBox Component e759. Adding and Removing an Item in a JComboBox Component e760. Selecting an Item in a JComboBox Component with Multiple Keystrokes e761. Determining If the Menu of a JComboBox Component Is Visible e762. Displaying the Menu in a JComboBox Component Using a Keystroke e763. Displaying the Menu in a JComboBox Component Using a Keystroke If the Selected Item Is Not Unique e764. Setting the Number of Visible Items in the Menu of a JComboBox Component e765. Listening for Changes to the Selected Item in a JComboBox Component e766. Listening for Action Events from a JComboBox Component © 2002 Addison-Wesley. |