2012-02-08 66 views
2

我需要在Jtable單元格中添加圖像而不使用TableCellRenderer.If我使用下面的代碼表示它在該特定單元格中顯示名稱(字符串)而不是image.how來執行此操作。在不使用渲染器的情況下在JTable中添加圖像

ImageIcon Icon= new ImageIcon("Blank.gif"); 
table.setValueAt(Icon,1,0); 


using renderer 

class FRM_FLXD_ICON_ASSGN extends DefaultTableCellRenderer { 
     ImageIcon Icon; 
    public Component getTableCellRendererComponent(
     JTable table, Object value, boolean selected, boolean focus, 
     int row, int col) { 
     if(selected == true){ 
      Icon=new ImageIcon(getClass().getResource("Pointer.gif")); 
     } 
    else{ 
      Icon=new ImageIcon(getClass().getResource("Blank.gif")); 
    } 
     this.setIcon(Icon); 
     return this; 
    } 

} 
+3

歡迎論壇:-)當你在學習,請學習Java命名約定以及和堅守下去 – kleopatra 2012-02-08 11:26:49

+1

*「不使用渲染器」* ***爲什麼?*** – 2012-02-08 11:28:13

+0

@AndrewThompson如果我使用渲染器意味着我創建單獨的類,所以。 – javalearner 2012-02-08 11:30:37

回答

3

JTable知道Icon/ImageIcon對象,那麼你可以直接添加Icon/ImageIconJTableexample

+0

上述顯示圖像的編碼需要哪些更改? – javalearner 2012-02-08 11:20:51

+0

請檢查我的編輯代碼示例 – mKorbel 2012-02-08 11:21:32

+0

只是downvoted你的例子(我怎麼可能錯過了:-) – kleopatra 2012-02-08 11:25:49

相關問題