event
ItemListener example
In this example we are going to see how to use several components like JRadioButton
and JComboBox
bundled with an ItemListener
. As you know, radio buttons and combo boxes are quite popular in Graphical Applications because they give flexibility to the end users, and it’s very natural to them to interact with them when you want to give specific values to choose from.
In short, to use components with ItemListener
you have to:
- Create a new class that implements
ItemListener
interface. - Override the methods that correspond to the events that you want to monitor on the radio buttons e.g
itemStateChanged
and customize as you wish the handling of the respective event - Create a number of new
JRadioButtons
components. - Use the
addItemListener
to add theItemListener
to each one of the buttons. - Create
JComboBox
components and use theaddItemListener
to add theItemListener
to each one of the.
Let’s see the code snippet that follows:
package com.javacodegeeks.snippets.desktop; import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ButtonGroup; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; public class ItemListenerExample { public static void main(String args[]) { JFrame jFrame = new JFrame(); Container cPane = jFrame.getContentPane(); ItemListener itemListener = new ItemListener() { @Override public void itemStateChanged(ItemEvent event) { System.out.println("Source: " + getName(event.getSource())); System.out.println("Item: " + getName(event.getItem())); int state = event.getStateChange(); System.out.println("State: " + ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected")); } private String getName(Object o) { if (o instanceof JComponent) { JComponent jComponent = (JComponent) o; return jComponent.getName(); } else { return o.toString(); } } }; JPanel jPanel = new JPanel(new GridLayout(0, 1)); ButtonGroup buttonGroup = new ButtonGroup(); JRadioButton option = new JRadioButton("Option 1", true); option.setName(option.getText()); option.addItemListener(itemListener); buttonGroup.add(option); jPanel.add(option); option = new JRadioButton("Option 2", false); option.setName(option.getText()); option.addItemListener(itemListener); buttonGroup.add(option); jPanel.add(option); option = new JRadioButton("Option 3", false); option.setName(option.getText()); option.addItemListener(itemListener); buttonGroup.add(option); jPanel.add(option); cPane.add(jPanel, BorderLayout.NORTH); String itemArray[] = {"Item 1", "Item 2", "Item 3"}; JComboBox combobox = new JComboBox(itemArray); combobox.setName("Combo"); combobox.addItemListener(itemListener); combobox.setMaximumRowCount(4); cPane.add(combobox, BorderLayout.SOUTH); jFrame.pack(); jFrame.setVisible(true); } }
This was an example on how to work with ItemListener in Java.