I have a three column jtable and want to have the first two columns have an Imageicon and a string right next to it.
Currently I am displaying the ImageIcons like
DefaultTableModel model = new DefaultTableModel(rows, columns) {
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
case 1:
return ImageIcon.class;
case 2:
return String.class;
default:
return Object.class;
}
}
};
jTable.setModel(model);
I did find this but don't know what goes in ...
or how to set the ImageIcon and string in the jtable with this:
Java - Is it possible to put an image and a String in the same JTable cell?
Any help will be appreciated.
I did find this but don't know what goes in ... or how to set the ImageIcon and string in the jtable with this:
You need to create custom Object (lets say you call it MyCustomObject) to hold the information you want to display. So your custom Object will have two properties: Icon and Text.
Then you create the Object and add to the TableModel like you do for any other Object in the table.
You need to override the getColumnClass() method to return MyCustomObject
for the first two columns. Then you also need to set the custom renderer for the MyCustomObject
.
So then in the rendering code you would do something like:
MyCustomObject data = (MyCustomObject)value;
setIcon(data.getIcon());
setText(data.getText());