Controls and layout managers
Components
Event listeners
A frame that uses an action event listener to update the display based on the user's selection
The actionPerformed method of the ActionListener interface
Method | Description |
---|---|
void actionPerformed(ActionEvent e) | Invoked when an item is selected. |
The itemStateChanged method of the ItemListener interface
Method | Description |
---|---|
void itemStateChanged(ItemEvent e) | Invoked when an item is selected or deselected. |
Common methods of the ItemEvent class
Method | Description |
---|---|
getSource() | Returns the source of the event. |
getItem() | Returns the selected item. |
getStateChanged() | Returns an int value that indicates whether an item was selected or deselected. The field names for these values are SELECTED and DESELECTED. |
Code that creates a combo box
products = getProducts();
productComboBox = new JComboBox();
for (Product p : products)
productComboBox.addItem(p.getDescription());
productComboBox.setSelectedIndex(0);
productComboBox.addActionListener(this);
add(productComboBox);
Code that implements the ActionListener interface for the combo box
public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == productComboBox) { int i = productComboBox.getSelectedIndex(); Product p = products.get(i); priceTextField.setText(p.getFormattedPrice()); } }