繁体   English   中英

复选框渲染器在JTable列中不正确

[英]checkbox renderer incorrect in JTable's column

我成功地将复选框添加到JTable的列中。 但我想使用我的自定义复选框,我修改了BooleanRender以扩展我的自定义复选框,它可以工作。问题是当我选中复选框时它会显示默认的jCheckbox设计并显示我的coustom复选框,它也会在我未选中时发生复选框。 这是我之前提出的问题

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

}

首先,覆盖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();
    }
}

问题是,当我选中复选框时,它会立即显示默认的jCheckbox设计并显示我的coustom复选框,当我取消选中该复选框时也会发生这种情况。

首先来看看概念:编辑器和渲染器以及使用其他编辑器

就像您必须提供自定义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"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM