my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Using JCheckBox as CellRenderer in JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using JCheckBox as CellRenderer in JList" Watch "Using JCheckBox as CellRenderer in JList" New topic
Author

Using JCheckBox as CellRenderer in JList

Biliang Zhou
Ranch Hand

Joined: Jun 28, 2006
Posts: 43
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Jun 28, 2006
Posts: 43
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.
 
subject: Using JCheckBox as CellRenderer in JList