Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Swing, Trying to replace boolean check-box in a JTable with an image-icon checkbox
    text
    copied!<p>So I have a JTable with check-boxes. I would like to have the check-boxes contain one image when they are "checked" and another image when they are "unchecked" (i.e., display images instead of a checked or unchecked box). Is there a way to do this? I've tried fooling with the TableCellRenderer that returns a JLabel with an ImageIcon but it was not really very effective.</p> <p><strong>More specifically, when the box is checked or unchecked the right images are there, but when the user is changing the check-box state (while the mouse is down) the original checked/unchecked images appear</strong></p> <p>This is the TableCellRenderer I tried (I have also tried it with JPanels but this was ineffective as well</p> <pre><code>public class CrassusEventTableCellEyeRenderer extends JCheckBox implements TableCellRenderer { static Icon greyEye; static Icon blackEye; {//STATIC CODE BLOCK try { greyEye = new ImageIcon(ImageIO.read(new File("icons/shittyTest.png"))); blackEye = new ImageIcon(ImageIO.read(new File("icons/blackEye.png"))); } catch (IOException e) { greyEye = null; blackEye = null; } } public CrassusEventTableCellEyeRenderer(){ super(); this.addItemListener(new IsCheckedItemListener()); setIcon(greyEye); } //commented out code that I have tried in place of the IsCheckedItemListener /* @Override public void setSelected(boolean sel){ super.isSelected(); if(sel) setIcon(blackEye); else setIcon(greyEye); } */ public class IsCheckedItemListener implements ItemListener{ @Override public void itemStateChanged(ItemEvent e) { if(isSelected()) setIcon(blackEye); else setIcon(greyEye); } } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { System.out.println("value: "+value+", row: "+row+", column: "+column); if(value instanceof Boolean){ setSelected(((Boolean) value).booleanValue()); } return this; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload