I need to update an icon in JTable
after the user digit a number in a JTextField
.
Step-by-Step:
JTable
row, and after that, opens a JFrame
to edit its contents.JTextField
in that EditFrame
where the user put a number from 0 to 100. That number will change the icon being displayed in my JTable row after the user close EditFrame
.I read the documentation here.I changed my aproach.I killed the renderer and created static Icons in my Meal class and created a method like that:
public ImageIcon getIconByValue(int value){
if(value==0)
return ONESTAR;
else
return ...;
}
and made some changes in my tablemodel(to store a ImageIcon):
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case RATING:
return ImageIcon.class;
...}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//..other cases....//
case RATING:
meal.getIconByValue((int)aValue);
break;
}
public void setData(List<Meal> list){
this.meals.clear();
for(Meal m:list){
Meal meal=new Meal();
//...other sets..//
meal.getIconByValue(m.getRating());
}
this.meals.add(meal);
}
But my Rating space(where should show the stars), is totally blank.
Note: I read other topics too, but none of the helps how to change icons OnTheFly in a JTable
.
It was more simple than i expected.I loaded the images in my bean class like that:
private ImageIcon fivestars=new ImageIcon(getClass().getResource("/cr/hmp/gui/IMG/5star.png"));
Made my model to return an icon class in my getColumnClass:
case RATING:
return ImageIcon.class;
In my getValueAt i made:
return meal.getIconByValue( meal.getRating() );
And now updates the row after i close my EditFrame without any problem.