// // another example of the Java AWT, showing use of Checkbox and // CheckboxGroup classes. // see class definition and main method for details. // import java.awt.* ; import java.awt.event.* ; // // a CheckboxExample object is a CloseableFrame containing a number // of checkboxes, a Button, and a Label. depending on the // arguments to the constructor, the checkboxes may or may not be // grouped into a CheckboxGroup. when the user clicks on the // button, the text (labels) associated with the selected // checkboxes displayed in the Label. // class CheckboxExample extends CloseableFrame { Label display = new Label("", Label.CENTER) ; Checkbox[] boxes ; CheckboxGroup cbg ; CheckboxExample(boolean group, String[] items) { super("CheckboxExample") ; setLayout(new BorderLayout()) ; if (group) cbg = new CheckboxGroup() ; else cbg = null ; add(display, "North") ; // make checkboxes (with checkbox group if needed) // and lay out using panel. Panel p1 = new Panel() ; boxes = new Checkbox[items.length] ; for (int i = 0 ; i < items.length ; i ++) { if (group) boxes[i] = new Checkbox(items[i], cbg, (i==0)) ; else boxes[i] = new Checkbox(items[i]) ; boxes[i].addItemListener(new ItemListener() { // action to be performed when user // selects a checkbox. public void itemStateChanged(ItemEvent e) { clearDisplay() ; } } ) ; p1.add(boxes[i]) ; } add(p1, "Center") ; Button showB = new Button("display selections") ; showB.addActionListener(new ActionListener() { // action to be performed when user clicks on // button. public void actionPerformed(ActionEvent e) { resetDisplay() ; } } ) ; add(showB, "South") ; pack() ; show() ; } // clears display. // should probably be declared private, but calling a // private method from an anonymous inner class (as above) // apparently triggers a compiler bug, causing the // compiler to hang!). void clearDisplay() { display.setText("") ; } // displays current selection(s). // should probably be declared private, but calling a // private method from an anonymous inner class (as above) // apparently triggers a compiler bug, causing the // compiler to hang!). void resetDisplay() { String selected ; if (cbg != null) { // if boxes are grouped into a CheckboxGroup // (only one at a time can be selected), // show selected box's label. selected = cbg.getSelectedCheckbox().getLabel() ; } else { // if boxes are not grouped (any number can be // selected), display labels of all // selected boxes. StringBuffer sb = new StringBuffer() ; for (int i = 0 ; i < boxes.length ; i++) { if (boxes[i].getState()) { sb.append(boxes[i].getLabel()) ; sb.append(' ') ; } } selected = sb.toString() ; } display.setText("selected: " + selected) ; } // // ---- main --------------------------------------------------- // // command-line arguments are y/n (enclose in CheckboxGroup), // labels for Checkboxes. // public static void main(String[] args) { if (args.length < 2) { System.out.println("Arguments are y/n, items") ; System.exit(-1) ; } boolean group = args[0].equals("y") ; String[] items = new String[args.length-1] ; for (int i = 0 ; i < items.length ; i++) items[i] = args[i+1] ; CheckboxExample c = new CheckboxExample(group, items) ; } }