Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a list with checkboxes in Java Swing?

What would be the best way to have a list of items with a checkbox each in Java Swing?

I.e. a JList with items that have some text and a checkbox each?

like image 861
Shabaz Avatar asked Aug 21 '08 12:08

Shabaz


People also ask

Which is the swing checkbox class?

JCheckBox inherits JToggleButton class. Constructor of the class are : JCheckBox() : creates a new checkbox with no text or icon.


1 Answers

A wonderful answer is this CheckBoxList. It implements Telcontar's answer (though 3 years before :)... I'm using it in Java 1.6 with no problems. I've also added an addCheckbox method like this (surely could be shorter, haven't used Java in a while):

public void addCheckbox(JCheckBox checkBox) {     ListModel currentList = this.getModel();     JCheckBox[] newList = new JCheckBox[currentList.getSize() + 1];     for (int i = 0; i < currentList.getSize(); i++) {         newList[i] = (JCheckBox) currentList.getElementAt(i);     }     newList[newList.length - 1] = checkBox;     setListData(newList); } 

I tried out the demo for the Jidesoft stuff, playing with the CheckBoxList I encountered some problems (behaviors that didn't work). I'll modify this answer if I find problems with the CheckBoxList I linked to.

like image 63
Dan Rosenstark Avatar answered Oct 08 '22 19:10

Dan Rosenstark