Java ItemEvent SELECTED

Syntax

ItemEvent.SELECTED has the following syntax.

public static final int SELECTED

Example

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


import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
//ww  w.j ava 2  s  .  c  o  m
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) {
    JCheckBox cb = (JCheckBox) ie.getItem();
    int state = ie.getStateChange();
    if (state == ItemEvent.SELECTED){
      System.out.println(cb.getText() + " selected");
    }else{
      System.out.println(cb.getText() + " cleared");
    }
  }
  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);
  }
}




















Home »
  Java Tutorial »
    java.awt.event »




AdjustmentListener
ActionEvent
ActionListener
HierarchyEvent
HierarchyListener
InputEvent
ItemEvent
ItemListener
KeyAdapter
KeyEvent
KeyListener
MouseAdapter
MouseEvent
MouseListener
MouseMotionAdapter
MouseMotionListener
MouseWheelEvent
MouseWheelListener
WindowAdapter
WindowEvent
WindowFocusListener
WindowStateListener