0

I have a JTable which I create dynamically from List<String> objects. I do this probably completely wrong but it works. The only thing I can't get to work is adding Images to some of the cells.

All it does is, it adds the ImageIcon Object name as String to the cells. See my code below.

private static Image doneImage = getIconImage("doneImage");
private static Image notDoneImage = getIconImage("notDoneImage");

private DefaultTableModel model = new DefaultTableModel(){
    @Override
    public Class<?> getColumnClass(int column){
        if ((column & 1) != 0 ){
            return ImageIcon.class;
        }else{
            return String.class;
        }
    }
};

initTables();

JTable table = new JTable();
table.setModel(model);

private void initTables(){  

    model.addRow(new Object[]{});

    int rowsToAdd = 0;
    int rowCount = 0;
    int columnId = 0;

    for(HouseObject aHouse : houses){
        for(RoomObject aRoom : aHouse.getRooms()){

            model.addColumn(null);
            model.addColumn(aRoom.getId());
            model.setValueAt(aRoom.getId(), 0, columnId);

            if (rowCount < aRoom.getEvents().size()){
                rowsToAdd = aRoom.getEvents().size() - model.getRowCount();
                for(int i = 0; i <= rowsToAdd; i++){
                    model.addRow(new Object[]{});
                }
                rowCount = model.getRowCount();
            }

            for(int i = 0; i < aRoom.getEvents().size(); i++){
                model.setValueAt(aRoom.getEvents().get(i).getId(), i+1, columnId);

                for(String houseDone : housesDone){
                    if(aRoom.getEvents().get(i).getId().contains(houseDone)){
                        model.setValueAt(doneImage , i+1, columnId+1); // this does not work
                    }else{
                        model.setValueAt(notDoneImage, i+1, columnId+1);
                    }
                }
            }

            columnId = columnId+2;
        }
    }
}
omnomnom
  • 190
  • 1
  • 12

2 Answers2

1

You need to install renderer for your table

Here is the renderer:

public class IconTableCellRenderer extends DefaultTableCellRenderer {

    @Override
    protected void setValue(Object value) {
        if (value instanceof Icon) {
            setText(null);
            setIcon((Icon) value);
        } else {
            super.setValue(value);
        }
    }
}

And so you must install it:

JTable table = new JTable();
table.setModel(model);
table.setDefaultRenderer(ImageIcon.class, new IconTableCellRenderer());
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • This works. I already had a cell renderer for font color, but I didn't get it that I have to override `setValue()`. Thank you! – omnomnom Nov 20 '17 at 10:09
  • @omnomnom, There is no need for the custom renderer. All you need to do is return "Icon.class" and the table will use its default Icon renderer. The table will use the default renderer which may (or may not) be different for each LAF. Don't create a renderer unless you really need to. – camickr Nov 20 '17 at 15:44
1

I have a JTable which I create dynamically from List objects.

Well you can't just add Strings to the table since then image will need to be added as an ImageIcon. So you would need a List so you can add String and Icon values.

Then you need to override the getColumnClass(...) method of your TableModel to return Icon.class for the column that contains the Icon. The table will then use the appropriate renderer for the Icon.

See: How to set icon in a column of JTable? for a working example.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I have overridden `getColumnClass()` and it is returning `ImageIcon.class` for all columns containing icons in my code. – omnomnom Nov 20 '17 at 09:57