2

I need to add image in Jtable cell without using TableCellRenderer.If i Use the following code means it display the name (string) in that particular cell instead of image.how to do this?.

 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;
     }

}
javalearner
  • 103
  • 5
  • 11
  • 3
    welcome to the forum :-) While you are at learning, please learn java naming conventions as well and stick to them – kleopatra Feb 08 '12 at 11:26
  • 1
    *"without using renderer"* ***Why?*** – Andrew Thompson Feb 08 '12 at 11:28
  • @AndrewThompson if i use renderer means i create separate class,so that. – javalearner Feb 08 '12 at 11:30
  • So what? What is the problem? If code creates another class, or causes it to be created (by default, or by using another class), it is a trifling thing. If code creates so many extra objects that it throws an `OutOfMemoryError`, that is a problem. – Andrew Thompson Feb 08 '12 at 11:41
  • @AndrewThompson 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; } } if i use this, the blank image set whole table column.just i click one column it will display pointer.gif – javalearner Feb 08 '12 at 11:47
  • @AndrewThompson how to change ,if i select one row the pointer.gif will display in that particular cell.then i click another row the pointer.gif will show in that place. the previous select cell will display Blank.gif.How to prevent to set blank.gif in all cell of column(0) – javalearner Feb 08 '12 at 11:55
  • I cannot read code in comments. Please edit it into your question, select the code and click the `{}` button above the message editing form. – Andrew Thompson Feb 08 '12 at 12:01
  • @Andrew I added renderer code in question. – javalearner Feb 08 '12 at 12:15

1 Answers1

3

JTable know Icon/ImageIcon Object, then you can add Icon/ImageIcon directly to the JTable, example

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319