I have some problems with the manage of two JTable, and relative data.
I had make this GUI:
"I want know a way for use a Icon in jTable but DON'T want handle the icon like a part of the informations. I want that the jTable realizes by itself the kind of vehicle and change the icon of conseguency"
You're going to have a really tough time trying to do it with this:
Object[] vehicle = {"aaa", "kind1", "marca", "disponibile", "ptt" };
Better to use the other one, with the value for the image columns. Remember, you can have null values.
First things first, this:
Object[] vehicle = {"/image/truck.png" ,"aaa", "kind1",
"marca", "disponibile", "ptt" };
Can be this
Object[] vehicle = {new ImageIcon(...) ,"aaa", "kind1",
"marca", "disponibile", "ptt" }
Since, you've overridden getColumnClass()
, the default renderer will render the column as an image, as ImageIcon.class
will be detected. See How to use Tables: Editors and Renderers.
As for the main question, how you can change the image dynamically based on the kind of vehicle change, you can override setValueAt
of the table model. Something like
DefaultTableModel model = new DefaultTableModel(...) {
private static final int CAR_TYPE_COLUMN = 2;
private static final int IMAGE_COLUMN = 0;
@Override
public void setValueAt(Object value, int row, int col) {
if (col == CAR_TYPE_COLUMN) {
ImageIcon icon = findImageByColumnCarType(value);
super.setValueAt(icon, row, IMAGE_COLUMN);
}
super.setValueAt(value, row, col);
}
public Class> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
};
Where findImageByColumnCarType
is some method to find the ImageIcon
based on the value
. setValueAt
will be called by the editor, in your case the combobox. So when the value is being set, the value of combo box will be passed to the setValueAt
, and you can use that to call the method findImageByColumnCarType
to get the ImageIcon
. You could have a Map
or something that you use to hold the icons and corresponding values. You can have the method return null
for a car type that has no image
Once you have the ImageIcon
you want, it's just a matter of calling super.setValueAt
to set the new icon for the image column for that same row.