///////////////////////// Simple Check Box Example //////////////////// /* Three check boxes are used in this example. The label of the last one checked will be displayed after the submit button is pressed. Multiple check boxes can be chosen, but only the last one checked will be displayed. If none was checked just before the submit button was clicked, then the answer to life, the universe, and everything will be displayed! */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimpleCheckBoxExample extends JFrame implements ActionListener, ItemListener{ JCheckBox cbLife = new JCheckBox("Life", false); JCheckBox cbUniverse = new JCheckBox("Universe", false); JCheckBox cbEverything = new JCheckBox("Everything", false); JButton submit = new JButton("Submit"); JLabel result = new JLabel(); String answer = new String(); ///////////////// Constructor method ///////////////////////////////// public SimpleCheckBoxExample(){ super("Simple Listener Example"); //Displays the name of the frame setSize(600,600); //x,y demensions of the frame in pixels // Closes the frame when the X at the top right corner is clicked setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowLayout flo = new FlowLayout(FlowLayout.CENTER, 10, 10); setLayout(flo); add(cbLife); // Add Life check box add(cbUniverse); // Add Universe check box add(cbEverything); // Add Everything check box add(submit); // Add the submit button add(result); // The the result label // Allows the SimpleCheckBoxExample to listen for the Submit button clicks submit.addActionListener(this); /* Registered events for the check boxes. * This allows SimpleCheckBoxExample to listen for check and unchecks * of any of the 3 checkboxes. */ cbLife.addItemListener(this); cbUniverse.addItemListener(this); cbEverything.addItemListener(this); setVisible(true); //Displays the frame } ////////////////// Action Listener Method ///////////////////////// /* Listener to take in any actions performed by user * such as button clicks, Mouse Clicks, and Enter Key press, to name * a few. * This method is called when the submit button is clicked. It simply * displays the varaible "answer" in the label field called "result". */ public void actionPerformed(ActionEvent event){ result.setText(answer); // Display the contents of the "answer" variable. } ////////////////// Item Listener Method ///////////////////////// /* Listener to take in any actions performed by user to an item * such as check boxes and Combo Boxes. * Here, when a check box is selected, this method is called. It then * assigns the label of the check box to the string variable "answer". */ public void itemStateChanged(ItemEvent ie){ JCheckBox cb = (JCheckBox) ie.getItem(); int state = ie.getStateChange(); if (state == ItemEvent.SELECTED){ answer = cb.getText(); // Gets the label of the check box that's selected } else{ answer = "42"; // If no text box has been selected, then the answer is 42. } } public static void main(String[] arguments) { //Crates an instance of the frame SimpleCheckBoxExample myFrameSetup = new SimpleCheckBoxExample(); } }