为什么从未调用DefaultTableCellRenderer?

时间:2018-11-12 16:01:40

标签: jtable render tablemodel

我很困惑为什么从未调用类ImageRenderer()?我通过控制台进行了检查,但没有打印出“类ImageRenderer在这里”。有什么特定的方法可以调用该类吗?

...
private JTable createTable(BufferedImage[] images){
    String[] headers = {"Img1","Img2","Img3","Img4","img5" };
    final Object[][] allData = {images,images,images,images,images};
    int rows = 1, cols = 5;     
    Object[][] data = new Object[rows][cols];

    for(int row=0; row<rows; row++) 
        for(int col = 0; col < cols; col++) { 
            data[row][col] = allData[row][col];                 
        }

    //create the model
    DefaultTableModel model = new DefaultTableModel(data, headers) {            
        public Class getColumnClass(int col) {
            return allData[col][0].getClass();
            }                       
        public boolean isCellEditable(int row, int col){
            return allData[col][row] != allData[0][row];
            }                  
     };

     //create the table
     JTable table = new JTable(model);      

     // I called the "class ImageRenderer" here.    
     table.setDefaultRenderer(String.class, new ImageRenderer());
     ...
     ...
     } );        

    return table;            
    }

我试图调用此类,但失败了

class ImageRenderer extends DefaultTableCellRenderer {  
    public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row, int column){
    super.getTableCellRendererComponent(table, value, isSelected,hasFocus, row, column);        

    //this code to test whether the "class ImageRenderer" get called or not
    System.out.println("The class ImageRenderer is here");

    setIcon(new ImageIcon((BufferedImage)value));
    setHorizontalAlignment(JLabel.CENTER);
    setText("");
    return this;
    }
 }

0 个答案:

没有答案