I’m trying to properly render and edit boolean values in my JTable
Generally, it works fine
However, there are a few things that bother me
One of them is this. To check my check box, you need to press the left mouse button. To uncheck it, you need to release it
Since it doesn’t really have to do with renderers or editors, my MRE is a simple frame with a check box containing panel
// snippet 1
package demos.button;
import di.Icons;
import javax.swing.*;
import java.awt.*;
public class CheckBoxDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Check Box demo");
JPanel mainPanel = createMainPanel();
frame.setContentPane(mainPanel);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
private static JPanel createMainPanel() {
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.CENTER);
JPanel mainPanel = new JPanel(layout);
mainPanel.add(createCheckBox());
return mainPanel;
}
private static JCheckBox createCheckBox() {
JCheckBox checkBox = new JCheckBox();
checkBox.setIcon(Icons.unchecked());
checkBox.setSelectedIcon(Icons.checked());
return checkBox;
}
}
// snippet 2
package di;
import util.IconUtil;
import javax.swing.*;
public class Icons {
private Icons() {
}
public static Icon checked() {
return IconUtil.findIcon("/checked.gif").orElse(null);
}
public static Icon unchecked() {
return IconUtil.findIcon("/unchecked.gif").orElse(null);
}
}
// snippet 3
package util;
import javax.swing.*;
import java.net.URL;
import java.util.Optional;
public class IconUtil {
private IconUtil() {
}
public static Optional<Icon> findIcon(String path) {
URL iconUrl = IconUtil.class.getResource(path);
return Optional.ofNullable(iconUrl).map(ImageIcon::new);
}
}
checked.gif
checked icon
unchecked.gif
unchecked icon
It’s not reproducible if I don’t set custom icons
// snippet 4
private static JCheckBox createCheckBox() {
JCheckBox checkBox = new JCheckBox();
return checkBox;
}
It’s all the more surprising as the documentation for setIcon()
clearly says
Sets the button’s default icon. This icon is also used as the “pressed” and “disabled” icon if there is no explicitly set pressed icon.
Since I set the unchecked icon as the default icon, it should be displayed whenever the checkbox is in the pressed state
However, the selected icon is
You may set the borderPainted
property to true
to see it even more clearly. It’s always visually selected when it’s pressed
// snippet 5
private static JCheckBox createCheckBox() {
JCheckBox checkBox = new JCheckBox();
checkBox.setIcon(Icons.unchecked());
checkBox.setSelectedIcon(Icons.checked());
checkBox.setBorderPainted(true);
return checkBox;
}
It’s as if I invoked
// snippet 6
checkBox.setIcon(Icons.checked());
instead of
// snippet 7
checkBox.setIcon(Icons.unchecked());
check box demo: pressed icon is selected
It’s interesting and I encourage you to share your take on this weird bug, but at the end of the day what I really want is this:
- Once pressed, keep my custom icon (checked or unchecked)
- But make the check box look “pressed” (paint the inner shadow border)
Basically, the same thing that happens in a default JCheckBox
(see snippet 4), but with a custom icon
Heneh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.