• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[JTable] Keep row color when sorting

 
Francois Dereims
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody.
I'm using a JTable to display student class objects.
Users can select a student and add him in a basket (to print some properties further). This basket is defined as a map <Integer, Student> where Integer is the student's row number in the JTable.
To avoid sorting issues caused by users manipulation, I convert the row coordinates from the view to the model like this:


Then, to add a student:


Now, i'd like to set a different row color in the JTable for the students in the basket.
This is how I do:


... and I can't get it work !!! Some lines are green and other not, with no reason !
What i want is to color the rows corresponding to the students in the basket even when the table is sorted.
I'm surely missing somethin'... Some idea woud be helpfull.
 
Brian Cole
Author
Ranch Hand
Posts: 886
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Francois Dereims wrote:
To avoid sorting issues caused by users manipulation, I convert the row coordinates from the view to the model like this: ...
  • modelRow = jtCandidats.convertRowIndexToModel(viewRow);

  • ...

    What i want is to color the rows corresponding to the students in the basket even when the table is sorted.


    Your renderer confuses me. It is ignoring the row parameter and using the modelRow field set elsewhere, right? How do know in what order the table cells will be renderered? (In general it's impossible to know. For one, it can depend on how the user manipulates other windows [even non-java ones] on the screen.)

    But the renderer's getTableCellRendererComponent() method has a row parameter, and also a table parameter, so it can just go ahead and just do
  • if (basket.get(table.convertRowIndexToModel(row))!=null)
  • without having to care about modelRow.
     
    Francois Dereims
    Greenhorn
    Posts: 11
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Brian, it walks ok now.

    To be honest, I'm not at ease with the renderer functions... but why are java table so complex ?

    Regards,
     
    Brian Cole
    Author
    Ranch Hand
    Posts: 886
    1
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Francois Dereims wrote:To be honest, I'm not at ease with the renderer functions... but why are java table so complex ?


    The idea of the cell renderer is kind of complex; that it's a "rubber stamp" that transiently paints the cells as needed. That can't really be helped without hurting performance.

    But if you take a look at the code itself (of the correct renderer, not the first one) don't you have to admit that it's pretty simple?
     
    Consider Paul's rocket mass heater.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic