Updated March 31, 2023
Introduction to Java ItemListener
ItemListener mainly used for item selection, and it is an interface that listens for the item event. The interface ItemListener basically manages the on or off status for one or several items. The Java ItemListener notifies each and every click on the checkbox, and it let you know against the ItemEvent. The package used for the ItemListener was found in the java.awt.event. The method used by the ItemListener was ItemStateChanged() and getState().
Methods used in ItemListener Interface
Methods used in the itemlistener interface are given below:
- itemStateChanged(ItemEvent e): The ItemStateChanged(ItemEvent e) method is called only when the item is checked or unchecked on the user’s registered checkbox component.
- getState(): The getState() method is the predefined method for the class of checkbox, by using this getState() method we can check whether the checkbox selected. It returns the result as true when the checkbox selected, and it returns false.
- Java ItemListener will use the method itemStateChanged(); this method will invoke the registered checkbox component at every check/uncheck and the syntax as follows,
- void itemStateChanged(ItemEvent e): The method triggered only when an item been clicked or un-licked by the user.
How Java ItemListener Works?
It implements an interface called ItemListener, by setting implementation of its method as follows,
ItemStateChanged(ItemEvent e): the method will be invoked only when an item been selected or deselected by the user. The ItemEvent will be even a checkbox, or it may be a list, the event source will make a call of its method by addItemListener(ItemListener e), where the object of the class implemented by its interface called ItemListener whenever a click made on the specific source it registers the class corresponds to the ItemEvent. The AWT package was imported to provide the GUI (Graphical User Interface) for the program. The ItemEvent class has several methods: public Object getItem(): Which returns the item when it was triggered or clicked the ItemEvent.
- public ItemSelectable getItemSelectable(): Which returns when an item been clicked.
- public int getStateChange(): Which returns the current/latest state of the item been clicked.
Let’s see the example for handling the ItemEvent by using the interface ItemListener; in the below code, we creating the class which listens to the ItemEvent by implementing the interface ItemListener. When a click is made like check/uncheck, the ItemEvent will be generated.
Code:
import java.awt.*;
import java.awt.event.*;
public class ItemListenerDemo implements ItemListener
{
Frame jFrame;
Checkbox chkBox1, chkBox2;
Label label_11;
ItemListenerDemo()
{
jFrame= new Frame("Java Item Listener Example");
chkBox1 = new Checkbox("ANDROID");
chkBox2 = new Checkbox("NETWORKING");
label_11 = new Label();
jFrame.add(chkBox1);
jFrame.add(chkBox2);
chkBox1.addItemListener(this);
chkBox2.addItemListener(this);
jFrame.setLayout(new FlowLayout());
jFrame.setSize(220,150);
jFrame.setVisible(true);
}
public void itemStateChanged(ItemEvent ie)
{
Checkbox ch =(Checkbox)ie.getItemSelectable();
if(ch.getState()==true)
{
label_11.setText(ch.getLabel ()+ " is checked");
jFrame.add(label_11);
jFrame.setVisible(true);
}
else
{
label_11.setText(ch.getLabel ()+ " is unchecked");
jFrame.add(label_11);
jFrame.setVisible(true);
}
}
public static void main(String... ar)
{
new ItemListenerDemo();
}
}
Output:
When you select an event checkbox, the ItemEvent is called, and you get a display message in which the checkbox is selected or deselected. For example, when you select the checkbox with Label – ANDROID, you will get the message as ‘ANDROID is checked’.
When you deselect the checkbox, the ItemEvent get called, and you will get the notified message with unchecked the checkbox with the name as ‘ ANDROID is unchecked ‘.
Examples to Implement Java ItemListener
The ItemListener interface has the class which processes under the implementation of ItemEvent. When the event occurs, the method will be invoked called itemStateChanged(). The class objects will be registered by the addItemListener() method, which is registered with a component. ItemListener mostly used for item selection, and it’s an interface that listens for the item event. The interface ItemListener, on the whole, manages the on or off status for one or several items. The Java ItemListener lets know each and every click on the checkbox, and it let you know against the ItemEvent. The package used for the ItemListener was found in the java.awt.event. The method used by the ItemListener was ItemStateChanged(), which will be called only when the item is checked or unchecked on the registered checkbox component by the user.
Example #1
Code:
import java.awt.*;
import java.awt.event.*;
public class ItemListener_Example implements ItemListener{
Checkbox checkbox_1,checkbox_2;
Label label_1;
ItemListenerExample(){
Frame f= new Frame("CheckBox Demo Program");
label_1 = new Label ();
label_1.setAlignment(label_1.CENTER);
label_1.setSize(400,100);
checkbox_1 = new Checkbox("DotNet Programming");
checkbox_1.setBounds(100,100, 50,50);
checkbox_2= new Checkbox("SQL Server");
checkbox_2.setBounds(100,150, 50,50);
f.add(checkbox_1);
f.add(checkbox_2);
f.add(label_1);
checkbox_1.addItemListener(this);
checkbox_2.addItemListener(this);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
} public void itemStateChanged(ItemEvent e) {
if(e.getSource()==checkbox_1)
label_1.setText("Checkbox: "
+ (e.getStateChange()==1?"checked":"unchecked"));
if(e.getSource()==checkbox_2)
label_1.setText("Checkbox Item Selection: " + (e.getStateChange()==1?"checked":"unchecked"));
}
public static void main(String args[])
{
new ItemListener_Example();
}
}
When executing the code, you will get the output as shown, the checkbox ItemEvent is called, and you get a display message in which checkbox is selected or deselected. For example, when you select the checkbox, you will get the message as ‘checked’.
Output:
Example #2
Code:
import java.awt.*;
import java.awt.event.*;
public class JavaItemListenerDemo {
private Frame mainFrame_1;
private Label headerLabel_1;
private Label statusLabel_1;
private Panel controlPanel_1;
public JavaItemListenerDemo()
{
organizeGUI();
}
public static void main(String[] args){
JavaItemListenerDemo itemListenerDemo = new JavaItemListenerDemo();
itemListenerDemo .showItemListener();
}
private void organizeGUI(){
mainFrame_1 = new Frame("Java Item Listener Example");
mainFrame_1.setSize(400,400);
mainFrame_1.setLayout(new GridLayout(3, 1));
mainFrame_1.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel_1 = new Label();
headerLabel_1.setAlignment(Label.CENTER);
statusLabel_1 = new Label();
statusLabel_1.setAlignment(Label.CENTER);
statusLabel_1.setSize(350,100);
controlPanel_1 = new Panel();
controlPanel_1.setLayout(new FlowLayout());
mainFrame_1.add(headerLabel_1);
mainFrame_1.add(controlPanel_1);
mainFrame_1.add(statusLabel_1);
mainFrame_1.setVisible(true); }
private void showItemListener(){
headerLabel_1.setText("Action Performed in Listener: ItemListener");
Checkbox chkAndroid = new Checkbox("ANDROID");
Checkbox chkNet = new Checkbox("NETWORKING");
Checkbox chkPhp = new Checkbox("PHP");
chkAndroid.addItemListener(new CustomItemListener());
chkNet.addItemListener(new CustomItemListener());
chkPhp.addItemListener(new CustomItemListener());
controlPanel_1.add(chkAndroid);
controlPanel_1.add(chkNet);
controlPanel_1.add(chkPhp);
mainFrame_1.setVisible(true);
}
class CustomItemListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
statusLabel_1.setText(e.getItem()
+" Checkbox: " + (e.getStateChange()==1?"checked":"unchecked"));
}
}
}
Output:
Recommended Articles
This is a guide to Java ItemListener. Here we also discuss the Introduction and how java item listener works? Along with different examples and their code implementation. You may also have a look at the following articles to learn more –