Check Boxes

 

Introduction.  Checkboxes are typically used when the developer wants the user to select any of the options in a list.  For example, say you are filling out a form that has a list of potential reasons for wanting to join the website.  The checkboxes likely give a list of what the developer anticipates might be important to potential members.  But it also allows the user to check any or all of the items listed.

The following table contains a list of the constructors that are available for the JCheckBox class.

 

Constructor Description
JCheckBox(String) This will put the specified String as a label for the checkbox
JCheckBox(String, boolean) This will put the specified String as a label for the checkbox and initialize it as selected if the boolean argument is true
JCheckBox(Icon) This will put the specified icon as a label for the checkbox
JCheckBox(Icon, boolean) This will put the specified icon as a label for the checkbox and initialize it as selected if the boolean argument is true
JCheckBox(String, Icon) This will put the specified String as a label along with the Icon as a  label
JCheckBox(String, Icon, boolean) This will put the specified String as a label along with the Icon as a  label and initialize it as selected if the boolean argument is true

 

The other thing we need to consider when making use of checkboxes is how the program can sense that the user has interacted with them.  To reiterate a previous page we will make use of ItemListeners.

Listing What's Checked.  Now we present our first example, an application called CheckedBoxList.java.  This program allows the user to check particular check boxes and then gives a list of those that are checked.  It makes use of all of the most important methods associated with ItemListeners.

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CheckedBoxList extends JFrame
{

private JTextArea outputArea;
private JLabel lblSelectInterests;
private JCheckBox chkJava, chkCPP, chkJavaScript, chkVB, chkNetworking;

public CheckedBoxList( )
{

super("Listing Checked Boxes");

Container c = getContentPane();
c.setLayout(new GridLayout(7,1));

lblSelectInterests = new JLabel("Please check interests:");
c.add(lblSelectInterests);

// create checkbox objects
chkJava = new JCheckBox("Java");
c.add(chkJava);

chkCPP = new JCheckBox("C++");
c.add(chkCPP);

chkJavaScript = new JCheckBox("JavaScript");
c.add(chkJavaScript);

chkVB = new JCheckBox("Visual Basic");
c.add(chkVB);

chkNetworking = new JCheckBox("Networking");
c.add(chkNetworking);
//  adding the user action handlers to the check boxes
//  these make use of a user defined class CheckBoxHandler( )

CheckBoxHandler handler = new CheckBoxHandler( );
chkJava.addItemListener(handler);
chkCPP.addItemListener(handler);
chkJavaScript.addItemListener(handler);
chkVB.addItemListener(handler);
chkNetworking.addItemListener(handler);

outputArea = new JTextArea( );
c.add(outputArea);

addWindowListener( new WindowAdapter( )
{

public void windowClosing(WindowEvent e)
{

System.exit(0);

}

}
);

setSize(150,600);
show( );

}

public static void main(String args[])
{

new CheckedBoxList( );

}

private class CheckBoxHandler implements ItemListener
{

private String valJava = "";
private String valCPP = "";
private String valJavaScript = "";
private String valVB = "";
private String valNetworking = "";

public void itemStateChanged(ItemEvent e)
{

// depending on the source of the check different actions are taken
// depending on whether the check results in a selection or deselection
// different actions are taken

if (e.getSource( ) == chkJava)

if (e.getStateChange( ) == ItemEvent.SELECTED)

valJava = "Java\n";

else

valJava = "";

if (e.getSource( ) == chkCPP)

if (e.getStateChange( ) == ItemEvent.SELECTED)

valCPP = "C++\n";

else

valCPP = "";

if (e.getSource() == chkJavaScript)

if (e.getStateChange() == ItemEvent.SELECTED)

valJavaScript = "JavaScript\n";

else

valJavaScript = "";

if (e.getSource() == chkVB)

if (e.getStateChange() == ItemEvent.SELECTED)

valVB = "Visual Basic\n";

else

valVB = "";

if (e.getSource() == chkNetworking)

if (e.getStateChange() == ItemEvent.SELECTED)

valNetworking = "Networking\n";

else

valNetworking = "";

outputArea.setText(valJava + valCPP + valJavaScript + valVB + valNetworking);

// ends itemStateChanged

// ends CheckBoxHandler

// ends CheckBoxTest

 

The following image represents the GUI.

 

 

The code will be discussed in class.  The only thing that is particularly exotic is the ways it
  • handles the window closing
  • handles the checkbox actions

Both of these have their own sections of code.

Changing Font Properties.  We are now going to use a program that I modified from Deitel and Deitel that changes the properties of some text based on what the user checks.  It's got some fairly clever coding in it.  You should call the program CheckBoxTest.java.

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CheckBoxTest extends JFrame
{

private JTextField t;
private JCheckBox bold, italic;

public CheckBoxTest()
{
super("JCheckBox Test");

Container c = getContentPane();
c.setLayout(new FlowLayout());

t = new JTextField("Watch for the font style change",20);
t.setFont(new Font("TimesRoman",Font.PLAIN, 14));
c.add(t);

// create checkbox objects
bold = new JCheckBox("Bold");
c.add(bold);

italic = new JCheckBox("Italic");
c.add(italic);

CheckBoxHandler handler = new CheckBoxHandler( );
bold.addItemListener(handler);
italic.addItemListener(handler);

addWindowListener( new WindowAdapter()
{

public void windowClosing(WindowEvent e)
{

System.exit(0);

}

}
);

setSize(275,100);
show();

}

public static void main(String args[])
{

new CheckBoxTest( );

}

private class CheckBoxHandler implements ItemListener
{

private int valBold = Font.PLAIN;
private int valItalic = Font.PLAIN;

public void itemStateChanged(ItemEvent e)
{

if (e.getSource( ) == bold)

if (e.getStateChange( ) == ItemEvent.SELECTED)

valBold = Font.BOLD;

else

valBold = Font.PLAIN;

if (e.getSource( ) == italic)

if (e.getStateChange( ) == ItemEvent.SELECTED)

valItalic = Font.ITALIC;

else

valItalic = Font.PLAIN;

t.setFont(new Font("TimesRoman",valBold + valItalic, 14));
t.repaint( );

// ends itemStateChanged

// ends CheckBoxHandler

} // ends CheckBoxTest

 

 

The following image represents the GUI.

 

 

Notice how the displayed text changes whether it is italicized and/or bold based on the user selections.

The one truly new piece of code uses the setFont method to change its properties.

t.setFont(new Font("TimesRoman",valBold + valItalic, 14));

Notice how adding the constants valBold + valItalic changes the properties.  This is because you are actually adding integer constants that have been configured to accumulate attributes.