ItemListener in Java

Introduction

This article explains how the ItemListener interface in Java works. The Netbenas IDE is used for the development of the example.

What is ItemListener

ItemListener is an interface that listens for the item event, basically it refers to the item selection. The ItemListener interface mainly maintains an on/off state for one or more items.

Methods used in ItemListener Interface

  • itemStateChanged(ItemEvent e)

          This method is called when the item is selected or deselected by the user.

  • getState()

          It is a predefined method of the checkbox class, using this method we check whether the checkbox is 

          selected or not. If the checkbox is selected then it returns true otherwise it returns false.

Packages Imported

java.awt.*;

AWT stands for Abstract Window Toolkit, it is a package in Java that is imported to provide a graphical user interface to programs. The AWT classes falls under the following categories.

Containers

The containers consist of the following things:

  • Frame
  • Dialog
  • Window
  • Panel
  • ScrollPane

Components

The Components of AWT are as follows:

  • Button
  • Checkbox
  • Choice
  • List
  • Label
  • TextArea
  • PopupMenu

LayoutManager

The LayoutManager has the following things:

  • FlowLayout
  • Borderlayout
  • GridLayout
  • Gridbaglayout
  • Cardlayout

java.awt.event.*;

This package is imported to handle various types of events fired by AWT components. Events are basically fired by event sources.

Example

In this example; we define how to use the ItemListener interface in Java using the Netbeans IDE. There are certain steps in the Netbeans IDE that we need to follow as explained below.

Step 1

Open the Netbeans IDE.

fig 1.jpg

Step 2

Click on the "File" from the Menu bar as in the following:

fig 2.jpg

Step 3

Click on "New Project" as in the following:

fig 3.jpg

Step 4

Select "Java" and "Java application" as in the following:

fig 4.jpg

Step 5

Click on "Next" as in the following:

fig 5.jpg

Step 6

Instead of project name specify "CheckBoxTest" and instead of main class also specify "CheckBoxTest" and click on "Finish".

fig 6.jpg

Step 7

In this class write the following code (the class name is "CheckBoxTest"):

import java.awt.*;

import java.awt.event.*;

class CheckBoxTest extends Frame implements ItemListener

{

     Checkbox cb1,cb2;

     TextField tf;

     CheckBoxTest()

    {

     setTitle("Checkbox testing");

     setSize(300,300);

     setLayout(new FlowLayout());

     cb1=new Checkbox("c++");

     cb2=new Checkbox("java");

     tf=new TextField(20);

     cb1.addItemListener(this);

     cb2.addItemListener(this);

          add(cb1);

          add(cb2);

          add(tf);

        setVisible(true);

     }     

   public void itemStateChanged(ItemEvent e)

{

    if(e.getSource()==cb1 || e.getSource()==cb2)

    {

       if(cb1.getState()&&cb2.getState())

       tf.setText("{Both are Selected");

       else if(cb1.getState())

       tf.setText("c++ has been selected");

       else if(cb2.getState())

       tf.setText("java has been selected");

       else

       tf.setText("None selected");

     }

}

 public static void main(String[] args)

  {

    new CheckBoxTest();

   }

}

 

fig 7.jpg

 

fig 7a.jpg

Step 8

 

Now go to ChecKBoxTest and right-click on that, click on "Run" from the menu bar as in the following:

 

fig 8.jpg

 

Output

 

The output will appear as in the following:

 

fig 9.jpg

 

If we select the first item, C++, then the output will be "C++ has been selected" as in the following:

 

fig 10.jpg

 

If we select the second item, Java, then the output will be "Java has been selected" as in the following:

 

fig 11.jpg

 

If we select both items then the output will be "Both are selected" as in the following:

 

fig 12.jpg

 

If we do not select any item then the output will be "None are selected" as in the following:

 

fig 13.jpg

  

Up Next
    Ebook Download
    View all
    Learn
    View all