2016-04-08 8 views
0

私のGUIには、JCheckBoxとして表示されているブール型の列を持つJTableがあります。金属の色は私のGUIに合わないので、私は次のコードを使用:UIManagerとJCheckBoxアイコン

ImageIcon icon = new ImageIcon(MyGUI.class.getResource("resources/checkbox1.png")); 
UIManager.put("CheckBox.icon", icon1); 

私は私が望んでいた未選択チェックボックスを得たが、私が選択したJCheckBoxのをカスタマイズするために変更できることを示すUIManagerには鍵がありません。 選択したJCheckBoxビューをグローバルに変更する方法はありますか? P.P.私は不透明で透明な背景の両方を試しましたが、結果は同じです - チェックボックスは動作しません。

+0

[ルックアンドフィールの変更](http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/index.html)? – MadProgrammer

答えて

0

私はインターネット上で解決策を見つけたし、自分のためにそれを変更しました。

class CheckBoxIcon implements Icon { 
    public void paintIcon(Component component, Graphics g, int x, int y) { 
     AbstractButton abstractButton = (AbstractButton)component; 
     ButtonModel buttonModel = abstractButton.getModel(); 

     if(buttonModel.isSelected()) 
      g.drawImage(createImage("resources/checkbox2.png"), x, y, component); 
     else 
      g.drawImage(createImage("resources/checkbox1.png"), x, y, component); 
    } 
    public int getIconWidth() { 
     return 13; 
    } 
    public int getIconHeight() { 
     return 13; 
    } 

    protected Image createImage(String path) { 
     URL imageURL = CheckBoxIcon.class.getResource(path); 
     Image icn = null; 

     if (imageURL == null) { 
      if(null==icn){ 
       //System.out.println("path: "+path); 
       icn = new ImageIcon (MyGUI.class.getResource(path)).getImage(); 
       if(null!=icn) 
        return icn; 
       else{ 
        System.err.println("Resource not found: " + path); 
        return null; 
       } 
      } 
      return null; 
     } else { 
      return (new ImageIcon(imageURL)).getImage(); 
     } 
    } 
} 

コードでこの行を使用します。

UIManager.put("CheckBox.icon", new CheckBoxIcon()); 
関連する問題