• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Update an icon in JTable after user input.

 
Lee Dha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to do the following:
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.

But nothing happens... i read:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender and made the code:

My TableModel:

And my bean:

My Edit frame(most code cutted because is HUGE):

off course, i dont save the JLabel in my database, its only to view in the Jtable.
Note:all data are displayed correctly after change except the icons in Jtable.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lee,
Welcome to the Ranch!

In your model, why do you have a JLabel to represent rating? A model should only deal with data (like Integer, String etc.) and not with UI components (like JLabel, JComboBox etc.).
Same goes with your table model too. Why you returning JLabel.class in the getColumnClass() method? The model should only deal with data and not with UI components.

This is actually creating several problems for you. You are updating the 'rating' value indeed, but, it is a JLabel that actually represents this in your table model - which is actually not updated anywhere. You have fix this to solve your problems.

Also, whatever object you return from your getValueAt() is the one that is passed to the getTableCellRendererComponent() in the 'Object object' parameter. So, you get your data right there (so, actually there is no need to get your Meal object via the row number).

 
Lee Dha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranganathan,
thanks for the welcome!

I am a newbie with Java, still learning.I let JLabel in the model because i will not show the numbers(the getRating()), only the images.I need of getRating to change the image.If i cut JLabel in the model how my JTable will show the Icons?How would you do it?Post a small example.


cheers,
 
Rob Camick
Ranch Hand
Posts: 2599
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to create a custom renderer. Just store the Icon that you want to display in the TableModel. Then override the getColumnClass(...) method to return Class.Icon for that column and the JTable will use the default Icon renderer.
 
Lee Dha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to create a custom renderer. Just store the Icon that you want to display in the TableModel. Then override the getColumnClass(...) method to return Class.Icon for that column and the JTable will use the default Icon renderer.

But how i will make the exchange of Icons if my getRating is lower or higher than a given rate?My icons will be stars(one to five), and the getRating will tell what star will be displayed per row.
Rob, care to post a small code explained that?
 
Rob Camick
Ranch Hand
Posts: 2599
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe your Meal class defines static icons to represent your ratings. Then you have a method like getRatingIcon() that will return the appropriate icon.
 
Lee Dha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick ,
is ALMOST working.If i close my main frame(after i edit) and open, shows the icon changed correctly, but dont update the Icon after i update the values in my EditFrame.
In my Meal Class, i made the changes:

In my model i changed:

and

and setData(load the data toModel):


Where i am going wrong?
 
Lee Dha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where i am going wrong?

Damn...its worse... was showing the icons because i had that:


Without that line, the Rating place(where should show Icons) is a blank space.
 
Rob Camick
Ranch Hand
Posts: 2599
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I said you don't need a custom renderer. The table will choose the appropriate renderer based on the getColumnClass() method. JTable supports an Icon renderer.

 
Lee Dha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick,
i got that, but... what is the problem with my changing method?My bean(Meal class):

My problem is with getIconByValue.What is the correct point to call him to make the change in my model?
Thanks for you help, i am almost one week with that problem(all other data update fine, except the icons).
 
Rob Camick
Ranch Hand
Posts: 2599
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess the problem is with your getValueAt(...) method of the TableModel (although you didn't post the code so I don't know if this is indeed the problem). This is the method that needs to return the proper Icon.

The code for the column that contains the icon should probably be something like:



 
Lee Dha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick ,
i am doing that(yeah, i know, a bit recursive...) in myGetValueAt:

And nothing happens.

I should make getIconByValue in the tablemodel?
 
Lee Dha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick , IT WORKS!
Thanks for you help!

The problem wasn t in the model, and yes its strange but:
return meal.getIconByValue( meal.getRating() );
works fine!

My problem was:

I removed the static and use with getResource:

And works flawless!It update the Icon after i press Exit in my EditFrame!
Many thanks for your help.It was much less complex than using renderer
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FWIW, I would take a different approach. An Icon is a drawing resource, more than a mere image. An Icon implementation can accommodate state, thus obviating the need for multiple Icons.

Here's a SSCCE to show what I mean:With a double instead of an int for its 'value' and an image with transparent stars drawn over (i.e. after) a filled rectangle whose width represents the value, this could quite easily be tweaked to visually represent fractional stars. You can't do that with a small set of ImageIcons.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic