Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to show JCheckBox component as elements of a JList ?

 
Abhinav Anand
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I want to show a dialog with JList component in it. Now i want to display a JCheckBox component instead of a simple text label in the JList. So that it would enable my user to select/unselect the checkbox when the users click on an item in the JList.
I have found that there is no method to provide an editor for the the JList component. It is possible to provide an editor in the JTable and JTree.
Any suggetions are welcome.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recomend not using a JList for what you want to do. It just doesn't make sense really.
I asume that the reason you need JCheckBox's in a JList is so that you can have a large list and scroll through the list of JCheckbox elements. If you have another reason, I would like to know what it is.
However, if my assumption is correct you can put a JPanel in a JScrollPane. Place all your JCheckBoxes on the JPanel. Then you can scroll up and down the JPanel if you have that many JCheckBox elements.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal,
Here's a suggestion: Use a "JTable" with one column. Hide the "grid" lines (using method "setShowHorizontalLines()" in class "JTable"). Make the class of the column "Boolean" (see method "getColumnClass()" in interface "TableModel"). The default renderer for such a column is a "JCheckBox". Finally, place the "JTable" in a "JScrollPane". This should give you the desired effect.
Hope this has helped you.
Good Luck,
Avi.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example.
 
Abhinav Anand
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Thanks a lot for such a great tip and example, it definately works.
Will keep u guys posted.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic