Reputation: 555
I successful add checkbox into JTable's column. but I want to use my custom checkbox, i modified BooleanRender to extend my custom checkbox , and it works.the problem is when I selected the checkbox it will show default jCheckbox design at moment and show my coustom checkbox, it also happen when i unselected the checkbox. here is the question I asked before
class BooleanRenderer extends TriCheckBox implements TableCellRenderer, UIResource {
private static final long serialVersionUID = 1L;
private final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
BooleanRenderer() {
super();
setHorizontalAlignment(JLabel.CENTER);
setBorderPainted(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelected(value != null && ((Boolean) value).booleanValue());
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
} else {
setBorder(noFocusBorder);
}
return this;
}
}
public class TriCheckBox extends JCheckBox {
public static ImageIcon icon =new ImageIcon("src/checkbox_off.png");
public static ImageIcon smallIcon = new ImageIcon(icon.getImage().getScaledInstance (12, -1, Image.SCALE_SMOOTH));
public static ImageIcon iconDark =new ImageIcon("src/logo.png");
public static ImageIcon smallIconDark = new ImageIcon(iconDark.getImage().getScaledInstance (12, -1, Image.SCALE_SMOOTH));
public static ImageIcon iconBrown =new ImageIcon("src/checkbox_on.png");
public static ImageIcon smallIconBrown = new ImageIcon(iconBrown.getImage().getScaledInstance (12, -1, Image.SCALE_SMOOTH));
private boolean indeterminate;
@Override
public void paint(Graphics g) {
if (isSelected()) {
indeterminate = false;
}
if(indeterminate){
setIcon(smallIconDark);
}else if(isSelected()){
setIcon(smallIconBrown);
}else{
setIcon(smallIcon);
}
super.paint(g);
}
public boolean isIndetermainate() {
return indeterminate;
}
public void setIndetermainate(boolean indetermainate) {
this.indeterminate = indetermainate;
if (indetermainate) {
setSelected(false);
repaint();
}
}
}
Upvotes: 1
Views: 109
Reputation: 22474
First, it is not a good idea to override the paint(...)
method, if you want to do some painting, override the paintComponent(...)
method instead.
In this case, you're not doing any custom painting so you don't need to override any of these methods. Here is what you can do instead:
public class TriCheckBox extends JCheckBox {
....
public void updateState() {
if (isSelected()) {
indeterminate = false;
}
if(indeterminate){
setIcon(smallIconDark);
}else if(isSelected()){
setIcon(smallIconBrown);
}else{
setIcon(smallIcon);
}
}
public void setIndetermainate(boolean indetermainate) {
this.indeterminate = indetermainate;
if (indetermainate) {
setSelected(false);
}else{
updateState();
}
}
@Override
public void setSelected(boolean selected){
updateState();
}
}
Upvotes: 2
Reputation: 347184
problem is when I selected the checkbox it will show default jCheckbox design at moment and show my coustom checkbox, it also happen when i unselected the checkbox.
Start by taking a look at Concepts: Editors and Renderers and Using Other Editors
Just like the fact that you had to provide a custom TableCellRenderer
, you're going to have to provide a custom TableCellEditor
Then adding to what @Titus has suggested, you should not be referencing src
in your paths
public static ImageIcon icon =new ImageIcon("src/checkbox_off.png");
needs to become
public static ImageIcon icon =new ImageIcon(TriCheckBox.class.getResource("/checkbox_off.png"));
Upvotes: 1