![]() ![]()
|
2
CheckBox-List Component
Features of JCCheckBoxList
Classes
Properties
Methods
Examples
2.1 Features of JCCheckBoxList
A
JCCheckBoxListfunctions 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
JListcomponent that implements aJCheckBoxas the cell renderer.- To use a
com.klg.jclass.util.swing.JCCheckBoxListin 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 ofObjects (usuallyStrings), or aVectorof list items.- For comparison purposes, a
JCCheckBoxListis shown beside aJListin 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
JCCheckBoxListdoes 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
JCCheckBoxListdoes 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 aJListwith aJCCheckBoxListis the change of the component's name and this import line:jclass.util.swing.JCCheckBoxList;
Figure 2 : A JCCheckBoxList.
The code that instantiates a
JCCheckBoxListis:
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 ========JCCheckBoxListinherits all the properties ofJList, it also usesListSelectionEventandListSelectionListenerto let you track the list items that have been selected. The code fragment shown below, also taken fromexamples.elements.CheckBoxList, shows how to use theListSelectionEventto react when items in aJCCheckBoxListare 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;
}
}
}
![]() ![]()
|