File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes [JTable] Keep row color when sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "[JTable] Keep row color when sorting" Watch "[JTable] Keep row color when sorting" New topic
Author

[JTable] Keep row color when sorting

Francois Dereims
Greenhorn

Joined: May 01, 2008
Posts: 11
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

Joined: Sep 20, 2005
Posts: 862
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.


    bitguru blog
    Francois Dereims
    Greenhorn

    Joined: May 01, 2008
    Posts: 11
    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

    Joined: Sep 20, 2005
    Posts: 862
    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?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: [JTable] Keep row color when sorting