Public list () {
super(" AK check list ");
String[] listData = {"Apple", "Orange", "Cherry", "Blueberry", "Banana", "Hongmei" and "Watermelon"};
Try {
ui manager . setlookandfeel(ui manager . getsystemlookandfeelclassname());
Catch (exception e) {
System.out.println ("System look and feel not found");
}
final JList list checkbox = new JList(buildCheckBoxItems(list data . length));
The final JList listDescription = new jlist (listdata);
list description . setselectionmode(ListSelectionModel。 Single choice);
ListDescription. Add MouseListener (new MouseAdapter() {
public void mouse clicked(mouse event me){
if (me.getClickCount()! = 2) {
Return;
}
int selectedIndex = list description . location toindex(me . get point());
if(selectedIndex & lt; 0) {
Return;
}
CheckBoxItem item =(CheckBoxItem)list checkbox . get model()。 getElementAt(selectedIndex);
item.setChecked(! item . is checked());
list checkbox . repaint();
}
});
ListCheckBox.setCellRenderer (new checkbox renderer()););
list checkbox . setselectionmode(ListSelectionModel。 Single choice);
ListCheckBox.addMouseListener (new MouseAdapter() {
public void mouse clicked(mouse event me){
int selectedIndex = list checkbox . location to index(me . get point());
if(selectedIndex & lt; 0) {
Return;
}
CheckBoxItem item =(CheckBoxItem)list checkbox . get model()。 getElementAt(selectedIndex);
item.setChecked(! item . is checked());
list description . setselectedindex(selectedIndex);
list checkbox . repaint();
}
});
JScrollPane scroll pane = new JScrollPane();
scroll pane . setrowheaderview(list checkbox);
scroll pane . setviewportview(list description);
list description . setfixedcellheight(20);
list checkbox . setfixedcellheight(list description . getfixedcellheight());
list checkbox . setfixedcellwidth(20);
getContentPane()。 add(scroll pane); //,BorderLayout。 Center);
setSize(350,200);
set visible(true);
}
private checkbox item[]build checkbox items(int total items){
checkbox item[]checkbox items = new checkbox item[total items];
for(int counter = 0; Counter < total items; Counter++) {
checkbox items[counter]= new checkbox item();
}
Return to checkboxItems
}
Public static void main (strinargs []) {
Checklist = new checklist ();
check list . addwindowlistener(new window adapter(){
public void window closing(window event we){
system . exit(0);
}
});
}
Class checkbox item {
Private Boolean values are checked;
Public check box Item() {
isChecked = false
}
Public boolean isChecked() {
Return to isChecked
}
Public void setChecked (Boolean value) (
isChecked = value
}
}
/* render JCheckBox to JList's inner class */
Class CheckBoxRenderer extends JCheckBox to implement ListCellRenderer {
Public checkbox Renderer() {
set background(ui manager . getcolor(" list . text background "));
set foreground(ui manager . getcolor(" list . text foreground "));
}
Common component getlistcellrenderercomponent (jlistlistbox, Object obj, int currentindex,
Boolean isChecked, Boolean hasfocus) (
setSelected(((CheckBoxItem) obj)。 is checked());
Return this;
}
}
}
In addition, you can use a single-column table and hide the underlined rows, which is simpler.