0

I need to update an icon in JTable after the user digit a number in a JTextField.

Step-by-Step:

  • The user clicks in a JTable row, and after that, opens a JFrame to edit its contents.
  • There is a 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.

LeeP
  • 43
  • 1
  • 1
  • 6
  • 1) You started to go wrong halfway though point one, where a frame was opened. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) The icons should not be loaded `getTableCellRendererComponent` method. They should instead be pre-loaded and stored as attributes. 3) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 02 '13 at 21:01
  • Andrew, i didn t understood what you talked in 1).I have a frame where i open a EditFrame when i click in a row and digit a number.It must update that row image when i close the editframe, just that.2)Care to put an example(even with only two icons working) about you are trying to tell?If you search about that issue, all topics about that are unanswered. – LeeP Oct 02 '13 at 22:02
  • *"Care to put an example.."* Care to post an SSCCE? I might be bothered helping you out if you follow my advice. – Andrew Thompson Oct 02 '13 at 23:08
  • Andrew Thompson, i am going to update my code, but i doubt anyone is gonna read.Even shortened, is a lot of code to see. – LeeP Oct 03 '13 at 05:02
  • *"Even shortened, is a lot of code to see."* Rot! What you've described can be mocked up in 100-150 LOC. But as to 'read', don't expect people to read every line carefully. Those who like an SSCCE typically compile and run it in debug mode, and that leads them directly to the source of the error, if not the solution. – Andrew Thompson Oct 03 '13 at 05:20
  • I am sorry to give to much work for moderators..it was a pain to edit my own code.The Original code was over 20times bigger. – LeeP Oct 03 '13 at 05:42
  • What is now posted is a number of oddly mashed together bits of Java source files that *does not compile* because of that. Waste your own time if you like, but please don't waste mine.. – Andrew Thompson Oct 03 '13 at 05:48
  • Andrew Thompson, some way got ALL imports of my code together!Of course is not going to compile.A tag code working like phpBB could be usefull down here...Its my first time here. – LeeP Oct 03 '13 at 06:36
  • Edit with another approach, without using a Renderer. – LeeP Oct 03 '13 at 21:36

1 Answers1

0

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.

LeeP
  • 43
  • 1
  • 1
  • 6