I have the following problem: i currently have a few classes representing various plants and animals, which all have base class "Organism", they are part of a world (another class, which contains a "2D" container of Organisms or empty fields - kinda like a 2D map of a world). Only 1 organism can occupy a world slot at the same time.
What i want is to draw a map of this world. In order to do this i have created a JTable, which uses world's 2D container as a data source. Now the problem is, i don't know how to make the JTable use class's own methods in order to draw cells.
Each class has it's method which returns an image representing it:
@Override public ImageIcon drawing(){
return new ImageIcon("Guarana.png");
}
my table has following components available:
private String[] columnNames;
private Object[][] data;
I've created my own table model (following the tutorial here: https://docs.oracle.com/javase/tutorial/uiswing/components/table.html)
So the creation of table looks like this:
model = new MyTableModel(testowyString, organizmy);
JTable table = new JTable(model);
table.setFillsViewportHeight(true);
table.setDefaultRenderer(Organizm.class, new IconRenderer());
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(210, 0, 780, 900);
add(scrollPane);
As you can see i have tried to create my own IconRenderer (but for some reason the JTable still doesn't draw the icons):
public class IconRenderer extends DefaultTableCellRenderer {
ImageIcon sheepIcon = new ImageIcon("Owca.png");
ImageIcon wolfIcon = new ImageIcon("Wilk.png");
ImageIcon mouseIcon = new ImageIcon("Mysz.png");
ImageIcon chickenIcon = new ImageIcon("Kurka.png");
ImageIcon turtleIcon = new ImageIcon("Zolw.png");
ImageIcon grassIcon = new ImageIcon("Trawa.png");
ImageIcon guaranaIcon = new ImageIcon("Guarana.png");
ImageIcon emptyIcon = new ImageIcon("Pusty.png");
public IconRenderer() { super(); }
public void setValue(Organizm organizm) {
setIcon((organizm == null) ? ikonkaPusty : organizm.rysowanie());
}
}
(yes, the icons here are a part of my old code, when each class didn't return it's icon)
To sum up: i want to make it so when JTable draws a map it draws an icon using an existing method of a class which is linked to cell.