Different imageIcon in different cells in JTable

前端 未结 1 565
攒了一身酷
攒了一身酷 2021-01-25 16:37

I think I got the imageIcon to show up differently in each cell but for some reason when I compile it, the images don\'t show up. It displays the name of the image but the image

相关标签:
1条回答
  • 2021-01-25 16:44

    You can pass in the name of the image when you call the new ImageRenderer constructor (read this).

    public class Movies extends javax.swing.JFrame {
        public Movies() {
            initComponents();
            table.getColumnModel().getColumn(1).setCellRenderer(new ImageRenderer("1.jpg"));
            table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer("2.jpg"));
        }
    }
    
    class ImageRenderer extends DefaultTableCellRenderer {
        ImageIcon icon = null;    
    
        ImageRenderer(String iconName) {
            icon = new ImageIcon(getClass().getResource(iconName));
        }
    }
    
    0 讨论(0)
提交回复
热议问题