[英]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.