简体   繁体   中英

how to set the image in Jtable cell using renderer?

Am using the following code for display the image in JTable cell.I have two questions for add image in jtable cell.

1.if i click the first row in the table ,the "Pointer.gif"image will display in that row.when i click the second row the "pointer .gif" image will show in the second row ,the first row get the "Blank.Gif".Then i click third row the Pointer.gif will show in the third row ,the second row get the "blank.gif" from the first row.In this time the first row will get empty(it means the first row didn't get pointer.gif and blank.gif).how to do this?

2.second question is if i select the first row the pointer.gif will show inthat first row.when i click the second row the the pointer.gif will move the second row ,the first row get empty.how to do this?

my problem is,when i use the following code the "blank.gif" will display in the whole column.when i click particular row the "pointer.gif" will show in that place.

    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.jpg"));
       }
           this.setIcon(Icon);
           return this;
         }

    }
    call this class like,

    table1.getColumnModel().getColumn(0).setCellRenderer(new FRM_FLXD_ICON_ASSGN());

in your renderer getTableCellRendererComponent will return jLabel with icon.

class FRM_FLXD_ICON_ASSGN extends DefaultTableCellRenderer {
           JLabel jLabel;
       public Component getTableCellRendererComponent(
          JTable table, Object value, boolean selected, boolean focus, int row, int col) {
           jLabel = new JLabel();
           if(selected == true){
               jLabel.setIcon(new ImageIcon(ImageIO.read(new File("C://pointer.jpg"))));
           }
      else{
          jLabel.setIcon(new ImageIcon(ImageIO.read(new File("C://blank.jpg"))));
       }
           return jLabel;
         }
    }

I think loading the pictures every time the cell is rendered is discouraged and consumes a lot of RAM. Outsource that code. If you pack your imags within the .jar getResource() is right.

I would think of it like this.

ImageIcon pointer = new ImageIcon(getClass.getResource("pointer.jpg")));
ImageIcon blank = new ImageIcon(getClass.getResource("blank.jpg")));

class FRM_FLXD_ICON_ASSGN extends DefaultTableCellRenderer {
       JLabel jLabel;
   public Component getTableCellRendererComponent(
      JTable table, Object value, boolean selected, boolean focus, int row, int col) {
       if(selected == true){
           jLabel.setIcon(pointer);
       }
  else{
      jLabel.setIcon();
   }
       return jLabel;
     }
}

edit:

maybe it should be something more like that

ImageIcon pointer = new ImageIcon(getClass.getResource("pointer.jpg")));
ImageIcon blank = new ImageIcon(getClass.getResource("blank.jpg")));

class FRM_FLXD_ICON_ASSGN extends DefaultTableCellRenderer {
       JLabel jLabel;
   public Component getTableCellRendererComponent(
      JTable table, Object value, boolean selected, boolean focus, int row, int col) {
       if(table.isColumnSelected(column)){
           jLabel.setIcon(pointer);
       }else{
           jLabel.setIcon(blank);
       }
       return jLabel;
     }
}

and in you table you should attach it to the column you want to display the pointer only.

table.getColumnModel().getColumn(1).setCellRenderer(new YourRenderer());

Renderer returns

JTable table, Object value, boolean selected, boolean focus, int row, int col

each of these values could be testable, most of them settable (required good knowledge of Java...) meaning you can testing for

  • Object instanceOf Whatever

  • selected

  • focus

  • JTable's row & col

really time to look at prepareRenderer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM