Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

   Swing

JCheckBox

This tutorial describes the Swing JCheckBox class.

The JCheckBox Class

Plain JCheckBox

Image JCheckBox

Other JCheckBox Methods


The JCheckBox Class   

A checkbox exists to allow the user to select or deselect an option. You can use the  JCheckBox class to implement checkboxes.  You should not that you can also put check boxes in menus using the JCheckBoxMenuItem class.

There are no less than seven constructors that can be used to create a JCheckBox because you have the choice of creating a checkbox with a plain box area or a checkbox where the boxed area is replaced by an image.  Here are the constructors:-

  • JCheckBox()

Creates a checkbox with no text, no icon, unselected

  • JCheckBox(String s)

Creates a checkbox with the specified text, no icon, unselected

  • JCheckBox(String s, boolean b)

Creates a checkbox with the specified text, no icon, and selected  if b is true

  • JCheckBox(Icon m)

Creates a checkbox with no text, the specified image, unselected

  • JCheckBox(Icon m, boolean b)

Creates a checkbox with no text, the specified image, and selected  if b is true

  • JCheckBox(String s, Icon m)

Creates a checkbox with the specified text, the specified image, unselected

  • JCheckBox(String s, Icon m, boolean b)

Creates a checkbox with the specified text, the specified image, and selected  if b is true

 


Plain JCheckBox      

To create a plain unselected checkbox, use a line of code such as:- 

JCheckBox cloudCheckBox = new JCheckBox("Cloud");

If you want to create a selected  checkbox you can create it like this:- 

JCheckBox cloudCheckBox = new JCheckBox("Cloud", true);

Alternatively, you can set the selected  status using :- 

//Set box to be selected 

cloudCheckBox.setSelected (true);

//Set box to be unselected

cloudCheckBox.setSelected (false);

The applet below shows two checkboxes.  The first one was created with the selected status set to true.  The second was created with the selected status set to false.

~~~Applet~~~

 


Image JCheckBox          

We can specify images to be used in place of the standard selected and unselected box.  To do this we need to create an Icon.  We can use the ImageIcon class for this:-

ImageIcon imgUnselected = new ImageIcon (getImage

                             (getCodeBase(),"unselected.gif"));

then we can use the variable that references the ImageIcon object in the argument for the checkbox's constructor:-

JCheckBox cloudCheckBox = new JCheckBox("Cloud",imgUnselected);

The image specified at checkbox creation time is the image that will be used for the unselected state.  This means we have to specify another image to be used when the checkbox is in the selected  state.  To do this use code such as:-

ImageIcon imgSelected = new ImageIcon (getImage

                             (getCodeBase(),"selected .gif"));

cloudCheckBox.setSelectedIcon(imgSelected);

Don't forget to place your image file in the same folder as you class file.

The applet below shows two checkboxes.  I have specified an image of a tick and an image of a cross to be used in place of the standard selected and unselected box.

~~~Applet~~~

 

 

Important: --  Using images in applets?   Then read this to understand the lines of code like:- 

ImageIcon imgUnselected = new ImageIcon (getImage

                             (getCodeBase(),"unselected.gif"));

Don't forget to import the package java.net.*

 

 


Other JCheckBox Methods 

Here are some methods we can use with the JCheckBox class.  You should note that this is not a full list, there are lots of other methods. 

JCheckBoc inherits a lot of these methods from JComponent and AbstractButton.  You should check out the inherited JComponent and JAbstractButton class methods.

  • setText(String s)

Set the text displayed by the checkbox.

  • setHorizontalTextPosition(int i)

Set the horizontal position of the checkbox text relative to the checkbox image using as the argument: JCheckBox.LEFT, JCheckBox.CENTER or JCheckBox.RIGHT

  • setVerticalTextPosition(int i)

Set the vertical position of the checkbox's text relative to the checkbox's image using as the argument: JCheckBox.TOP, JCheckBox.CENTER or JCheckBox.BOTTOM

  • setSelected(boolean b)

Sets the checkbox to selected if b is true else set it to unselected . 

  • boolean isSelected()

Returns true if the checkbox is selected and false if is unselected . 

  • setSelectedIcon(Icon m)

Sets the image to be used when the checkbox is in the selected state. 

  • setRolloverIcon(Icon m)

  • setRolloverEnabled(boolean b)

Use setRolloverIcon(Icon m) with setRolloverEnabled(true)to make the checkbox display the specified icon when the cursor passes over it.

 

~Now try the activity~

Activity A

Using appropriate methods from the JCheckBox, JAbstractButton and JComponent class, create an applet with ...

  1. at least one panel to contain some checkboxes
  2. two checkboxes similar to the first applet shown on this page
  3. set the font color of the checkbox labels to any color of your choice, (apart from black).
  4. set the font of the checkbox labels to Times, italic and 10 points in size.  The Font class is explained in the Graphics tutorial.
  5. set the panel background color to any color of your choice

 

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002