Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Using JCheckBox as CellRenderer in JList  RSS feed

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

}

}

################################################################
 
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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!