The JCheckBox

The JCheckBox is useful for when you require, for example, a question that has only two possible answers (or states) (i.e., yes or no). Let's look at an example application that uses the JCheckBox.

Code Listing 9: Using the JCheckBox

start example
import java.awt.*; import java.awt.event.*; import javax.swing.*;     public class JCheckBoxExample extends JFrame implements     ActionListener {     public static void main(String[] argv)     {         JCheckBoxExample mainApp = new JCheckBoxExample();     }          public JCheckBoxExample()     {         super("JCheckBox Example");         setBounds(0, 0, 300, 300);         getContentPane().setLayout(null);         setDefaultCloseOperation(EXIT_ON_CLOSE);                         // Create the label...         label = new JLabel("Check box is not ticked");         label.setLocation(10, 10);         label.setSize(label.getPreferredSize());                         // Create a checkbox...         checkbox = new JCheckBox("Click the JCheckBox to change its             state", false);         checkbox.setLocation(10, 40);         checkbox.setSize(checkbox.getPreferredSize());                         // Add the action listeners         checkbox.addActionListener(this);                         // Add the objects to the content pane...         getContentPane().add(label);         getContentPane().add(checkbox);                  setVisible(true);     }          public void actionPerformed(ActionEvent e)     {         if(e.getSource() == checkbox)         {             if(checkbox.isSelected())             {                 label.setText("Check box is ticked");                 label.setSize(label.getPreferredSize());             }             else             {                 label.setText("Check box is not ticked");                 label.setSize(label.getPreferredSize());             }         }     }          JLabel label;     JCheckBox checkbox; } 
end example

When we execute the JCheckBox example application, we can expect to see the following:


Figure 11: Using the JCheckBox

If we click the check box in our example application, we can see the text "Check box is not ticked" changes to "Check box is ticked." This can be seen in the following screen shot:


Figure 12: This shows that we have successfully retrieved the state of the JCheckBox.

Let's now look at the code that we used to create the JCheckBox. First we create the JCheckBox by calling its constructor with two parameters, as follows:

checkbox = new JCheckBox("Click the JCheckBox to change its      state", false);

The first parameter specifies the text to appear to the right of the check box, informing the user as to its purpose. The second specifies whether we wish the check box to start checked (true) or unchecked (false).

Next, we set the size and location of the check box using the setSize and setLocation methods as follows:

checkbox.setLocation(10, 40); checkbox.setSize(checkbox.getPreferredSize());

Finally, to retrieve the state of the JCheckBox, we use the isSelected method, which returns true or false, telling us whether it is checked or unchecked, respectively.

Creating Image Check Boxes

As with the JButton, it is also possible to assign the JCheckBox custom images to display for the various states instead of the standard GUI. Let's look at a modified version of the previous example that assigns two images to the check box—one for the unselected state and one for the selected state.

Code Listing 10: Creating image check boxes

start example
import java.awt.*; import java.awt.event.*; import javax.swing.*;     public class ImageCheckBoxExample extends JFrame implements     ActionListener {     public static void main(String[] argv)     {         ImageCheckBoxExample mainApp = new ImageCheckBoxExample();     }          public ImageCheckBoxExample()     {         super("Image Check Box Example");         setBounds(0, 0, 300, 300);         getContentPane().setLayout(null);         setDefaultCloseOperation(EXIT_ON_CLOSE);                         // Create the label...         label = new JLabel("Check box is not ticked");         label.setLocation(10, 10);         label.setSize(label.getPreferredSize());                         // Create a checkbox...         checkbox = new JCheckBox("Click the JCheckBox to change              its state", new ImageIcon("unselected.gif"));         checkbox.setSelectedIcon(new ImageIcon("selected.gif"));         checkbox.setFocusPainted(false);         checkbox.setBorderPainted(false);         checkbox.setContentAreaFilled(false);         checkbox.setLocation(10, 40);         checkbox.setSize(checkbox.getPreferredSize());                         // Add the action listeners         checkbox.addActionListener(this);                         // Add the objects to the content pane...         getContentPane().add(label);         getContentPane().add(checkbox);                  setVisible(true);     }          public void actionPerformed(ActionEvent e)     {         if(e.getSource() == checkbox)         {             if(checkbox.isSelected())             {                 label.setText("Check box is ticked");                 label.setSize(label.getPreferredSize());             }             else             {                 label.setText("Check box is not ticked");                 label.setSize(label.getPreferredSize());             }         }     }          JLabel label;     JCheckBox checkbox; }
end example

Once we copy the images from the CD-ROM into the same directory as the source code and execute the application, the following can be seen:


Figure 13: Changing the way the JCheckBox is displayed with custom images

Let's now look at the code that we have changed to create the image check box.

First, we change the second parameter of the constructor to load in the default image for the check box that is displayed if no other image is relevant or has been specified. This can be seen in the following line of code:

checkbox = new JCheckBox("Click the JCheckBox to change its state",     new ImageIcon("unselected.gif"));

Once that is done, we specify the image that is to be displayed if the check box is selected by calling the setSelectedIcon method. This can be seen in the following line of code:

checkbox.setSelectedIcon(new ImageIcon("selected.gif"));



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net