home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-swing / CheckboxDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.2 KB  |  101 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import com.sun.java.swing.JCheckbox;
  20. import com.sun.java.swing.JPanel;
  21. import com.sun.java.swing.JFrame;
  22. import com.sun.java.swing.ChangeListener;
  23. import com.sun.java.swing.ChangeEvent;
  24.  
  25. /**
  26.  * An application that displays two JCheckboxes. 
  27.  */
  28. public class CheckboxDemo extends JPanel {
  29.     static JFrame frame;
  30.     static String first = new String("Button 1");
  31.     static String second = new String("Button 2");
  32.  
  33.     public CheckboxDemo() {
  34.     super(true);
  35.  
  36.     // Create the buttons.
  37.     JCheckbox firstButton = new JCheckbox(first);
  38.         firstButton.setKeyAccelerator('1'); 
  39.     firstButton.setActionCommand(first);
  40.     firstButton.setSelected(true);
  41.  
  42.     JCheckbox secondButton = new JCheckbox(second);
  43.         secondButton.setKeyAccelerator('2'); 
  44.     secondButton.setActionCommand(second);
  45.  
  46.         // Register a listener for the checkboxes.
  47.     CheckboxListener myListener = new CheckboxListener();
  48.     firstButton.addActionListener(myListener);
  49.     firstButton.addChangeListener(myListener);
  50.     firstButton.addItemListener(myListener);
  51.     secondButton.addActionListener(myListener);
  52.     secondButton.addChangeListener(myListener);
  53.     secondButton.addItemListener(myListener);
  54.  
  55.     add(firstButton);
  56.     add(secondButton);
  57.     }
  58.  
  59.  
  60.     /** Listens to the checkboxes. */
  61.     class CheckboxListener implements ItemListener, //only event type needed
  62.                    ActionListener, //for curiosity only
  63.                    ChangeListener {  //for curiosity only
  64.     public void itemStateChanged(ItemEvent e) {
  65.         System.out.println("ItemEvent received: " 
  66.                    + e.getItem()
  67.                    + " is now "
  68.                    + ((e.getStateChange() == ItemEvent.SELECTED)?
  69.                    "selected.":"unselected"));
  70.     }
  71.  
  72.     public void actionPerformed(ActionEvent e) {
  73.         String factoryName = null;
  74.  
  75.         System.out.print("ActionEvent received: ");
  76.         if (e.getActionCommand() == first) {
  77.         System.out.println(first + " pressed.");
  78.         } else {
  79.         System.out.println(second + " pressed.");
  80.         }
  81.     }
  82.  
  83.     public void stateChanged(ChangeEvent e) {
  84.         System.out.println("ChangeEvent received from: "
  85.                    + e.getSource());
  86.     }
  87.     }
  88.  
  89.     public static void main(String s[]) {
  90.          WindowListener l = new WindowAdapter() {
  91.              public void windowClosing(WindowEvent e) {System.exit(0);}
  92.          };
  93.  
  94.          frame = new JFrame("CheckboxDemo");
  95.          frame.addWindowListener(l);
  96.          frame.add("Center", new CheckboxDemo());
  97.          frame.pack();
  98.          frame.setVisible(true);
  99.     }
  100. }
  101.