Check boxes with item changed event. : CheckBox Button « Swing JFC « Java






Check boxes with item changed event.

  

import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class Main implements ItemListener {
  JCheckBox jcbControl = new JCheckBox("Translate");
  JCheckBox jcbOption1 = new JCheckBox("A");
  JCheckBox jcbOption2 = new JCheckBox("B");
  JCheckBox jcbOption3 = new JCheckBox("C");

  Main() {
    JFrame jfrm = new JFrame("Check Box Demo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(300, 200);

    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jcbOption1.setEnabled(false);
    jcbOption2.setEnabled(false);
    jcbOption3.setEnabled(false);

    jcbControl.addItemListener(new ItemListener() {

      public void itemStateChanged(ItemEvent ie) {
        if (jcbControl.isSelected()) {
          jcbOption1.setEnabled(true);
          jcbOption2.setEnabled(true);
          jcbOption3.setEnabled(true);
          System.out.println("enabled.");
        } else {
          jcbOption1.setEnabled(false);
          jcbOption2.setEnabled(false);
          jcbOption3.setEnabled(false);
          System.out.println("disabled.");
        }
      }
    });

    jcbOption1.addItemListener(this);
    jcbOption2.addItemListener(this);
    jcbOption3.addItemListener(this);

    jfrm.add(jcbControl);
    jfrm.add(jcbOption1);
    jfrm.add(jcbOption2);
    jfrm.add(jcbOption3);
    jfrm.setVisible(true);
  }

  public void itemStateChanged(ItemEvent ie) {
    JCheckBox cb = (JCheckBox) ie.getItem();

    if (ie.getStateChange() == ItemEvent.SELECTED)
      System.out.println(cb.getText() + " selected.");
    else
      System.out.println(cb.getText() + " cleared.");

    if (jcbOption1.isSelected())
      System.out.println("1");
    else if (jcbOption2.isSelected())
      System.out.println("2");
    else if (jcbOption3.isSelected())
      System.out.println("3");
    else
      System.out.println("None");
  }
  public static void main(String args[]) {
    new Main();
  }
}

   
    
  








Related examples in the same category

1.JCheckBox is a widget that has two states. On and Off.
2.Swing CheckBoxesSwing CheckBoxes
3.CheckBox Demo 2CheckBox Demo 2
4. How to use the check box button How to use the check box button
5.CheckBox MnemonicCheckBox Mnemonic
6.Bad Checkbox UIBad Checkbox UI
7.React to menu action and checkbox menuReact to menu action and checkbox menu
8.Swing CheckBox DemoSwing CheckBox Demo
9.CheckBox Item ListenerCheckBox Item Listener
10.Icon CheckBox DemoIcon CheckBox Demo
11.Flat CheckBoxFlat CheckBox
12.Customizing the Icons in a JCheckBox Component
13.Customize these disabled icons
14.Set pressed icon
15.Display an icon when the cursor is moved over the checkbox. This is called the rollover icon.
16.Adding an Icon to the Label of a JCheckBox Component
17.Getting and Setting the State of a JCheckbox Component
18.Creating a JCheckbox Component
19.Get or set the selection state of JCheckBox
20.Customize JCheckBox icons
21.Radio button, ComboBoxRadio button, ComboBox
22.Using CheckBox ActionListener to controll font