2016-04-15 35 views
1

我成功地將複選框添加到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(); 
    } 
} 

}

+0

1)見[檢測/修復一個代碼塊的吊閉括號](http://meta.stackexchange.com/q/251795/155831)爲一個問題,我不能再打擾定影。 2)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

+0

'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'修飾符(上面提到&鏈接)。 –

回答

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

你可能想看看'public void updateState(Graphics g){',因爲我非常確定這不是你的意思;) – MadProgrammer

+0

@MadProgrammer我已經複製了OP的'paint(....) '方法,並忘記刪除該參數。 – Titus

1

問題是,當我選擇它會顯示在此刻默認JCheckBox的設計和展示我coustom複選框的複選框,它也發生在我未被選擇的複選框。

開始採取看看Concepts: Editors and RenderersUsing Other Editors

就像一個事實,即你必須提供自定義TableCellRenderer,你將不得不提供自定義TableCellEditor

然後加入什麼@Titus曾建議,你不應該在你的路徑引用src

public static ImageIcon icon =new ImageIcon("src/checkbox_off.png"); 

需要BEC OME

public static ImageIcon icon =new ImageIcon(TriCheckBox.class.getResource("/checkbox_off.png"));