Java. Is it possible to put image and row in the same JTable cell?

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 image and string in SAME JTable?

The reason for this is that I have a status column in my JTable that currently contains either a green, amber, or red image. And in order to fulfill the design requirements I need to add some explanatory text next to each image (so the text next to the green image will be "Online", the text next to the amber image will be "Unknown" and the text next to the red image will be "Offline"). I need to do this in one column (or what looks / behaves like one column), not two columns.

I researched this but didn't find any information.

+2


source to share


1 answer


Yes.

You need to use native cell rendering. See How to use tables for details .

You actually have two options, you can just set the icon and text of the cell, otherwise you can use the render hint text ...



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());

      

+4


source







All Articles