JCheckBox: setEnabled(boolean b) : JCheckBox « javax.swing « Java by API






JCheckBox: setEnabled(boolean b)

  


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.new JCheckBox(Action a)
2.new JCheckBox(String text)
3.new JCheckBox(String text, boolean selected)
4.new JCheckBox(String text, Icon normal)
5.JCheckBox: addActionListener(ActionListener lis)
6.JCheckBox: addChangeListener(ChangeListener lis)
7.JCheckBox: addComponentListener(ComponentListener l)
8.JCheckBox: addItemListener(ItemListener lis)
9.JCheckBox: isSelected()
10.JCheckBox: setBorderPaintedFlat(boolean b)
11.JCheckBox: setDisabledIcon(Icon disabledIcon)
12.JCheckBox: setDisabledSelectedIcon(Icon disabledSelectedIcon)
13.JCheckBox: setIcon(Icon defaultIcon)
14.JCheckBox: setMnemonic(int mnemonic)
15.JCheckBox: setPressedIcon(Icon pressedIcon)
16.JCheckBox: setRolloverIcon(Icon i)
17.JCheckBox: setRolloverSelectedIcon(Icon rolloverSelectedIcon)
18.JCheckBox: setSelected(boolean b)
19.JCheckBox: setSelectedIcon(Icon i)
20.JCheckBox: setText(String text) (HTML)
21.JCheckBox: setToolTipText(String text)