我成功地將複選框添加到JTable列中。但我想使用我的自定義複選框,我修改了布爾值來擴展我的自定義複選框,它的工作原理。問題是,當我選中複選框時,它將顯示默認的jCheckbox設計,並顯示我的coustom複選框,這也發生在我未選中複選框。 here is the question I asked before複選框渲染器在JTable列中不正確
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();
}
}
}
1)見[檢測/修復一個代碼塊的吊閉括號](http://meta.stackexchange.com/q/251795/155831)爲一個問題,我不能再打擾定影。 2)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –
'public static ImageIcon icon = new ImageIcon(「src/checkbox_off.png」);'3)部署時應用程序資源將成爲嵌入式資源,所以現在開始訪問它們是明智的。 [tag:embedded-resource]必須通過URL而不是文件訪問。請參閱[信息。頁面爲嵌入式資源](http://stackoverflow.com/tags/embedded-resource/info)如何形成的URL。 4)使用'static'通常會導致問題,而不是解決它們。在發佈該MCVE之前刪除所有'static'修飾符(上面提到&鏈接)。 –