Can't add image using ImageIcon to jTable cell

后端 未结 1 810
太阳男子
太阳男子 2020-12-12 05:33

I trying to add image using ImageIcon class to jTable cell , but i get in the cell sun.awt.image.ToolkitImage@196a4632 where it suppos

相关标签:
1条回答
  • 2020-12-12 05:55

    You need to override getColumnClass() on the table model, and return ImageIcon.class for the column with the ImageIcon. If you don't, the renderer will show the toString(), as the default column class type will be Object. See How to use Tables: Editors and Renderers.

    For example

    ImageIcon icon=new ImageIcon(getClass().getResource("exit.png"));
    String[] columns={"Page No","Chapter","Image"};
    Object[][] rows={{1,4,icon},{2,7,icon}};
    
    DefaultTableModel model = new DefaultTableModel(rows, columns) {
        @Override
        public Class<?> getColumnClass(int column) {
            switch(column) {
                case 0:
                case 1: return Integer.class;
                case 2: return ImageIcon.class;
                default: return Object.class;
            }
        }
    };
    JTable table = new JTable(model);
    
    0 讨论(0)
提交回复
热议问题