This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

JCheckBox in a JList  RSS feed

Ranch Hand
Posts: 15304
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!
What are you doing? You are supposed to be reading this tiny ad!
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!