Java ItemListener Example

Java




// Java AWT Program to demonstrate
// Java ItemListener
import java.awt.*;
import java.awt.event.*;
  
// Driver Class
class CheckboxExample {
      // Main function
    public static void main(String[] args) {
        // Create a new frame with a title.
        Frame frame = new Frame("Checkbox Example");
          
        // Create a checkbox labeled "Enable Feature."
        Checkbox checkbox = new Checkbox("Enable Feature");
  
        // Create an instance of MyItemListener to handle checkbox events.
        MyItemListener itemListener = new MyItemListener();
          
        // Register the itemListener with the checkbox to listen for events.
        checkbox.addItemListener(itemListener);
  
        // Add the checkbox to the frame.
        frame.add(checkbox);
          
        // Set the frame size and layout.
        frame.setSize(300, 200);
        frame.setLayout(new FlowLayout());
          
        // Make the frame visible.
        frame.setVisible(true);
    }
}
  
class MyItemListener implements ItemListener {
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            // Respond when the checkbox is selected (checked).
            System.out.println("Feature is enabled.");
        } else {
            // Respond when the checkbox is deselected (unchecked).
            System.out.println("Feature is disabled.");
        }
    }
}


Output:

Inside terminal the message is shown like below:

In this example, we have create a very basic GUI cover that has a “Enable Feature” checkbox. The itemStateChanged method is called in response to the user’s selection or deselection of the checkbox, and we implement the ItemListener port in the MyItemListener class.



Java ItemListener in AWT

The Java ItemListener user interface in Java’s Abstract Window Toolkit (AWT) is a crucial component for managing user interactions with elements like checkboxes and option lists. In this article, we wish to search how the ItemListener interface is old in AWT to respond to exploiter input.

The ItemListener interface is divided into the java.awt.event package and extends the EventListener user interface. It plays a very material role in handling user actions such as selecting or deselecting items in components like checkboxes, choice lists, and checkbox groups. To utilize the ItemListener, you need to implement this interface in a class that processes the ItemEvent. The object of this class is then registered with the GUI portion that generates the ItemEvent exploitation of the addItemListener() method. When the user interacts with the part, the itemStateChanged method of the documented physical object is invoked to respond to the user’s actions. 

Similar Reads

Declaration of ItemListener

public interface ItemListener extends EventListener { void itemStateChanged(ItemEvent e);}...

Java ItemListener Example

Java // Java AWT Program to demonstrate // Java ItemListener import java.awt.*; import java.awt.event.*;    // Driver Class class CheckboxExample {       // Main function     public static void main(String[] args) {         // Create a new frame with a title.         Frame frame = new Frame("Checkbox Example");                    // Create a checkbox labeled "Enable Feature."         Checkbox checkbox = new Checkbox("Enable Feature");            // Create an instance of MyItemListener to handle checkbox events.         MyItemListener itemListener = new MyItemListener();                    // Register the itemListener with the checkbox to listen for events.         checkbox.addItemListener(itemListener);            // Add the checkbox to the frame.         frame.add(checkbox);                    // Set the frame size and layout.         frame.setSize(300, 200);         frame.setLayout(new FlowLayout());                    // Make the frame visible.         frame.setVisible(true);     } }    class MyItemListener implements ItemListener {     public void itemStateChanged(ItemEvent e) {         if (e.getStateChange() == ItemEvent.SELECTED) {             // Respond when the checkbox is selected (checked).             System.out.println("Feature is enabled.");         } else {             // Respond when the checkbox is deselected (unchecked).             System.out.println("Feature is disabled.");         }     } }...