//: c13:RadioCheckNew.java // From Thinking in Java, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 1999 // Copyright notice in Copyright.txt // Radio buttons and Check Boxes // import javax.swing.*; import java.awt.*; import java.awt.event.*; public class RadioCheckNew extends JApplet { JTextField t = new JTextField(20); JCheckBox[] cb = { new JCheckBox("Check Box 1"), new JCheckBox("Check Box 2"), new JCheckBox("Check Box 3") }; ButtonGroup group = new ButtonGroup(); JRadioButton cb4 = new JRadioButton("four"), cb5 = new JRadioButton("five"), cb6 = new JRadioButton("six"); // Checking the source: class ILCheck implements ItemListener { public void itemStateChanged(ItemEvent e) { for(int i = 0; i < cb.length; i++) { if(e.getSource().equals(cb[i])) { t.setText("Check box " + (i + 1)); return; } } } } // vs. an individual class for each item: class IL4 implements ItemListener { public void itemStateChanged(ItemEvent e) { t.setText("Radio button four"); } } class IL5 implements ItemListener { public void itemStateChanged(ItemEvent e) { t.setText("Radio button five"); } } class IL6 implements ItemListener { public void itemStateChanged(ItemEvent e) { t.setText("Radio button six"); } } public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); t.setEditable(false); cp.add(t); ILCheck il = new ILCheck(); for(int i = 0; i < cb.length; i++) { cb[i].addItemListener(il); cp.add(cb[i]); } group.add(cb4); group.add(cb5); group.add(cb6); cb4.addItemListener(new IL4()); cb5.addItemListener(new IL5()); cb6.addItemListener(new IL6()); cp.add(cb4); cp.add(cb5); cp.add(cb6); } public static void main(String[] args) { JApplet applet = new RadioCheckNew(); Frame frame = new Frame("RadioCheckNew"); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); frame.add(applet); frame.setSize(325, 150); applet.init(); applet.start(); frame.setVisible(true); } } ///:~