Hi, I am trying to use JCheckBox as CellRenderer in JList. All go well, and the JCheckBoxes show up in the JList, but I cannot select or unselect these JCheckBoxes. They just don't respond to any mouse or keyboard action...
public ComboBoxRenderer() { setOpaque(true); setHorizontalAlignment(LEADING); setVerticalAlignment(CENTER);
for(int i = 0; i < 10; i ++) { integerArray[i] = new Integer(i); } }
public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { //Get the selected index. (The index param isn't //always valid, so just use the value.) int selectedIndex = ((Integer)value).intValue();
return new JCheckBox("Test: " + selectedIndex); } }
[ January 24, 2007: Message edited by: Michael Dunn ]
Biliang Zhou
Ranch Hand
Joined: Jun 28, 2006
Posts: 43
posted
0
I see the reason why:
The JList needs a MouseListener to handle the checking and unchecking of the checkboxes. That's what is missing in my code. I thought so long as JCheckBoxes are used as the ListCellRenderer they are ready to behave like JCheckBoxes standing alone, and that's not how ListCellRenderer works...