Java - Is it possible to put an image and a String in the same JTable cell?

后端 未结 1 1257
礼貌的吻别
礼貌的吻别 2020-12-12 02:26

I know how to put a String into a JTable cell, and I know how to put an image into a JTable cell. But is it possible to put an image and a String into the SAME JTable cell?<

相关标签:
1条回答
  • 2020-12-12 02:35

    Yes.

    You need to use a custom cell renderer. Check out How to use Tables for more details.

    You actually have two choices, you could simply set the icon and the text of the cell or you could use the renderers tooltip text instead...

    public class IconTextCellRemderer extend DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table,
                                      Object value,
                                      boolean isSelected,
                                      boolean hasFocus,
                                      int row,
                                      int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            setText(...);
            setIcon(...);
            setToolTipText(...);
            return this;
        }
    }
    

    Of course you need to apply the renderer to the column...

    TableColumnModel tcm = table.getColumnModel();
    tcm.getColumn(x).setCellRenderer(new IconTextCellRemderer());
    
    0 讨论(0)
提交回复
热议问题