Java Swing Tutorial - Java ItemEvent ITEM_STATE_CHANGED








Syntax

ItemEvent.ITEM_STATE_CHANGED has the following syntax.

public static final int ITEM_STATE_CHANGED

Example

In the following code shows how to use ItemEvent.ITEM_STATE_CHANGED field.

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/*from  ww w  . j  av a 2  s  .  com*/
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel implements ItemListener {
  public Main() {
    JCheckBox cb = new JCheckBox("www.java2s.com");
    cb.addItemListener(this);
    add(cb);
  }
  public void itemStateChanged(ItemEvent ie) {
      System.out.println(ie.getID() == ItemEvent.ITEM_STATE_CHANGED);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.add(new Main());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}