![]() ![]() ![]() |
2
CheckBox-List Component
Features of JCCheckBoxList
Classes
Properties
Methods
Examples
2.1 Features of JCCheckBoxList
A
JCCheckBoxList
functions just like aJList
, 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.
- It is a subclass of the
JList
component that implements aJCheckBox
as the cell renderer.- To use a
com.klg.jclass.util.swing.JCCheckBoxList
in your application, simply ensure that the JClass Elements JAR is part of your CLASSPATH.- Constructors are of the no argument type, or a single argument consisting of an instance of a
ListModel
, an array ofObject
s (usuallyString
s), or aVector
of list items.- For comparison purposes, a
JCCheckBoxList
is shown beside aJList
in the following figure.
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 fromJList
.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 fromJList
. Selection is handled just like aJList
; you can choose one of three selection modes: single, block, or multiple block.
2.5 Examples
This example shows the use of a
import com.klg.JCCheckBoxList
, including the action taken when an item is checked. If you look at the full listing inexamples.elements.CheckBoxList
, you'll observe that the only modification needed to replace aJList
with aJCCheckBoxList
is the change of the component's name and this import line: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
//================== ListSelectionListener interface methods ========JCCheckBoxList
inherits all the properties ofJList
, it also usesListSelectionEvent
andListSelectionListener
to let you track the list items that have been selected. The code fragment shown below, also taken fromexamples.elements.CheckBoxList
, shows how to use theListSelectionEvent
to react when items in aJCCheckBoxList
are selected. In this example, a horoscope based on the selected item is placed in a text area.
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;
}
}
}
![]() ![]() ![]() |