File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JCheckBox in a JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JCheckBox in a JList" Watch "JCheckBox in a JList" New topic

JCheckBox in a JList

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

I am trying to make a JList of JCheckBoxes. Here is the code

It comiles and runs, and I can see the Checkboxes with the correct text, however, I can't click on the check boxes. When I click the checks, they don't get checked. Any ideas?

GenRocket - Experts at Building Test Data
Travis Kline

Joined: Jul 16, 2002
Posts: 14
1. Create an Object to retain the checkbox's state, generate accessors, override toString(), etc
public ListCheckBox(Object newObject) {
aObject = newObject;
isSelected = false;
then add this object to your data model
DefaultListModel aModel = new DefaultListMode();
aModel.addElement(new ListCheckBox(newElement));

2. In your list cell renderer's getListCellRendererComponent, change the state of the checkbox:

this.setSelected(((ListCheckBox) value).isSelected());
3. Add a mouselistener to your list and upon mouseClicked:
public void doCheck() {
ListCheckBox item =
(ListCheckBox) getSelectedValue();
This is should start you in the right direction but you'll probably have to do a lot of tweeking to get it to behave the way you want.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

"TK" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: JCheckBox in a JList
It's not a secret anymore!