aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes programatically editing jTable cell based on another cell value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "programatically editing jTable cell based on another cell value" Watch "programatically editing jTable cell based on another cell value" New topic
Author

programatically editing jTable cell based on another cell value

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
basically my requirement is:
given a JTable with two columns (name, stringColor) i want to display the name either with background color or font color accordingly to color found in string column

the examples i found in sun java tutorials were of no help, since when coding the tableCellRenderer the "value" refers to the present column, not to the other column
i guess this is feasible, but i have no idea how

can anyone give me a hand, please?

thanks in advance


java amateur
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
since when coding the tableCellRenderer the "value" refers to the present column, not to the other column


Yes, but you also have access to the entire JTable, which has access to all the data in the TableModel.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
let's look at sun java tutorial code:
setValue(Object value) : how do i grab next (or after next, for the matter) column value? that's where i need help, since i dont know from where to start...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
The setValue() method isn't used to change the foreground or background of the renderer.

Instead you should be overriding the getTableCellRendererComponent() method. The basic logic is:

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:The setValue() method isn't used to change the foreground or background of the renderer.

Instead you should be overriding the getTableCellRendererComponent() method. The basic logic is:

hi again Rob
i was caught in a "Aaack! You caught us with our pants down! Here we are, tinkering with the servers and you show up. How awkward! Try back in just a minute... Sincerely, the red faced JavaRanch staff."
of course - i agree with you
i've all my code in place, but what i dont get is how i get the value of another column - inside cell Renderer - to check and then customize this cell
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
table.getModel().getValueAt(...);
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
still cant find my way out to just "coloring" one single cell - not just the entire row (this code colors all the columns in every row):
i tried:

but allways get a NullPointerException
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

That's because renderer components are not actual components. They are used for painting only, and not added to the JTable. You can only make changes through the TableCellRenderer.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
gonna give it another try, but still i cant color just one single cell

anyway i also have a custom sorter and i noticed that when i sort the colored rows stay still (dont respond to the order modification), so if i sort an uneven number of times the information gets messy...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
I give up. Your code looks nothing like the code I posted.

I don't see the super.getTableCellRendererComponent(...). The point of this code is to reset the foreground/background to the default values for every cell.

Then you can customize the color for a specific cell.

I don't see the "return this" either. The point of that statement is to use the renderer for this cell only.

and i noticed that when i sort the colored rows stay still (dont respond to the order modification)


You will need to use the table.convertRowIndexToView(...) method to make sure your code is changing the view colors.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i didnt show all code
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i dont focus on sorting now
still, i notice a "neutral" painted yellow
my problem is visible: all the row is painted, when i just wanted the cell with name in it
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You are changing the background regardless of the column. You should add an if or switch statement to only change the color for the right column.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Prime wrote:You are changing the background regardless of the column. You should add an if or switch statement to only change the color for the right column.

great!
thank you

and where do i call this method:
?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
and where do i call this method:


You use it to create a new variable so you can access the data from the TableModel correctly:

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i read both TableRowSorter and how to use tables but still cant figure out how (and where) to use:

in order to sincronize sorter with view
can someone please tell me?

here's the running code:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Rob Camick wrote:
and where do i call this method:


You use it to create a new variable so you can access the data from the TableModel correctly:


Isn't it the other way around? convertRowIndexToView takes an index for the model and transforms it into the matching index for the table (view). convertRowIndexToModel does the inverse.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
my last post was made before reading Rob's answer

anyway it looks like it doesnt do the job; an example is sorting several times column "name" and observing that "Pierrot" isnt allways painted in yellow...
here's the updated code:
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
not receiving email notification can de infuriating
ok - i didnt get last message from Rob Prime, and now i agree with him, since my code works nicely if i use convertRowIndexToModel instead

a great thank you very much to both!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
Isn't it the other way around? convertRowIndexToView takes an index for the model and transforms it into the matching index for the table (view). convertRowIndexToModel does the inverse.


Yes, it nice to know that someone takes the time to understand "the intent" of the suggestion.

> not receiving email notification can de infuriating

What I find infuriating is not doing a little thinking on your own. Every step of the way in this posting suggestions have had to be repeated. Take some time to understand the suggestions and what that mean. Every time I've hade to post sample code. This last time I made a typo and you weren't able to fix that either. If you had read the API description of the method you might have noticed the problem and even noticed the other method. You don't learn anything by expecting us to post working code all the time.

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:
Isn't it the other way around? convertRowIndexToView takes an index for the model and transforms it into the matching index for the table (view). convertRowIndexToModel does the inverse.


Yes, it nice to know that someone takes the time to understand "the intent" of the suggestion.

> not receiving email notification can de infuriating

What I find infuriating is not doing a little thinking on your own. Every step of the way in this posting suggestions have had to be repeated. Take some time to understand the suggestions and what that mean. Every time I've hade to post sample code. This last time I made a typo and you weren't able to fix that either. If you had read the API description of the method you might have noticed the problem and even noticed the other method. You don't learn anything by expecting us to post working code all the time.

i cant disagree more
i did read the API - if you carefully read my posts you would not say such a thing
so, i did come across the other mehtod - the one you call a typo - and used it like written in the API and elsewhere, and i got no positive results; after all by myself i would never ever reached the solution - that's why forums are great

as to your last sentence, i'm no java student, just a self taught amateur who makes applications for himself, though i do have to work a lot to get it all together, as you might imagine - no teachers, no friends who know java, no one to talk to, just google, this forum and a few books

you have to understand that one can only reason upon some data if he has the required knowleadge, no other way around, despite any amount of thinking one uses on any problem

as a last note, i find it a bit infuriating too having to deal with people who seem to not accept the fac that we all are humans - people that make mistakes, like me and you: a few weeks ago you literally tortured me for hours and hours till you finally grabbed my idea (after my 10th post!), idea which was clearly defined in the fourth line of my initial post, remember?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: programatically editing jTable cell based on another cell value