我成功地将复选框添加到JTable的列中。但我想使用我的自定义复选框,我修改了BooleanRender以扩展我的自定义复选框,它可以工作。问题是当我选中复选框时它会显示默认的jCheckbox设计并显示我的coustom复选框,它也会在我未选中时发生复选框。 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();
}
}
}
答案 0 :(得分:2)
首先,覆盖paint(...)
方法不是一个好主意,如果你想做一些绘画,请改写paintComponent(...)
方法。
在这种情况下,您没有进行任何自定义绘制,因此您无需覆盖任何这些方法。以下是您可以做的事情:
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();
}
}
答案 1 :(得分:1)
问题是,当我选中该复选框时,它会立即显示默认的jCheckbox设计并显示我的coustom复选框,当我取消选中该复选框时也会发生这种情况。
首先看看Concepts: Editors and Renderers和Using Other Editors
就像您必须提供自定义TableCellRenderer
一样,您必须提供自定义TableCellEditor
然后添加@Titus建议的内容,您不应在路径中引用src
public static ImageIcon icon =new ImageIcon("src/checkbox_off.png");
需要成为
public static ImageIcon icon =new ImageIcon(TriCheckBox.class.getResource("/checkbox_off.png"));