JClass Elements

PreviousNextIndex

2

CheckBox-List Component

Features of JCCheckBoxList  Classes  Properties  Methods  Examples


2.1 Features of JCCheckBoxList

A JCCheckBoxList functions just like a JList, except that a check box appears to the left of the list items. (See Figure 2.)

Figure 1 :  Comparison of a JCCheckBoxList with Swing's default JList.


2.2 Classes

com.klg.jclass.util.swing.JCCheckBoxList - The component itself.

javax.swing.event.ListSelectionListener - for listening to changes in the list.


2.3 Properties

JCCheckBoxList does not need any extra properties. You are free to use all the properties it inherits from JList.

For a full listing of the properties, see Appendix A, Bean Properties Reference.


2.4 Methods

JCCheckBoxList does not need any extra methods. You are free to use all the methods it inherits from JList. Selection is handled just like a JList; you can choose one of three selection modes: single, block, or multiple block.


2.5 Examples

This example shows the use of a JCCheckBoxList, including the action taken when an item is checked. If you look at the full listing in examples.elements.CheckBoxList, you'll observe that the only modification needed to replace a JList with a JCCheckBoxList is the change of the component's name and this import line:

import com.klg.jclass.util.swing.JCCheckBoxList;

Figure 2 :  A JCCheckBoxList.

The code that instantiates a JCCheckBoxList is:


public JCCheckBoxList list;

list = new JCCheckBoxList(data);
list.setBorder(new EtchedBorder(Color.black, Color.yellow));
list.setForeground(Color.white);
list.setSelectionMode(0);
list.addListSelectionListener(this);
add(list);

Since JCCheckBoxList inherits all the properties of JList, it also uses ListSelectionEvent and ListSelectionListener to let you track the list items that have been selected. The code fragment shown below, also taken from examples.elements.CheckBoxList, shows how to use the ListSelectionEvent to react when items in a JCCheckBoxList are selected. In this example, a horoscope based on the selected item is placed in a text area.

//================== ListSelectionListener interface methods ========

public void valueChanged(ListSelectionEvent e) {

int index = list.getSelectedIndex();

switch(index) {
case 0:
horoscope.setText(
"A homeless puppy will follow you home. Be good to it.");
break;
...
More messages for the JTextArea
...
case 19:
horoscope.setText("You're running low on supplies. Run out & stock up.");
break;

}

}

}



PreviousNextIndex