• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using JCheckBox as CellRenderer in JList  RSS feed

 
Biliang Zhou
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

Here's the source code:

Thank you for looking into this issue!

################################################################

import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class TestRenderer extends JPanel{

JList list;
Integer integerArray[] = new Integer[10];

public TestRenderer()
{
super();

list = new JList(integerArray);
ComboBoxRenderer renderer= new ComboBoxRenderer();
list.setCellRenderer(renderer);

this.add(list);
}

class ComboBoxRenderer extends JCheckBox implements ListCellRenderer {

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);
}
}

private static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new TestRenderer();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});

}

}

################################################################
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
working example here

http://www.objects.com.au/java/examples/swing/CheckBoxList2.do

[edit]
looks like the link is now defunct

here's the code


[ January 24, 2007: Message edited by: Michael Dunn ]
 
Biliang Zhou
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

Thank you, Michael!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!