Forum Stats

  • 3,750,837 Users
  • 2,250,259 Discussions
  • 7,867,173 Comments

Discussions

Sort rows in JTable except the last row

843806
843806 Member Posts: 49,998
edited Apr 14, 2009 11:21AM in Swing
Hi All,
I have a JTable, which contains columns with numbers. The last row contains column sums.
I want to be able to sort rows, but the last row with sums must remain motionless.

Does anyone know the solution of this problem?
Thanks in advance.
«1

Comments

  • 843806
    843806 Member Posts: 49,998
    I assume you are using the new sortable tables in Java 1.6+? I'm not too familiar with those yet, as I still code for 1.5, so I'll just throw out some ideas without examples.

    Probably the easiest way I can think of is to just keep a handle on the sums row, and after each sort, move it back to the bottom.
  • 843806
    843806 Member Posts: 49,998
    Yes, I'm using java 6 se.
    Please send me some examples if you can.

    About handle. Seems like that's a good solution, but also it would be much better if there was some example of it. I don't know how to move the row back to the bottom after sort.
    Thank you.
  • 843806
    843806 Member Posts: 49,998
    Why not just make sure your comparator recognises the last row and returns +1 regardless.
  • 843806
    843806 Member Posts: 49,998
    Sorry, I'm using default comparator in TableRowSorter.
    Are you suggesting to use another comparator?
  • 843806
    843806 Member Posts: 49,998
    sabre150 wrote:
    Why not just make sure your comparator recognises the last row and returns +1 regardless.
    This crossed my mind, but wouldn't it move the sums row to the top when you sort by the same column again for a reverse order? Presumably, you would still want sums at the bottom.
  • 843806
    843806 Member Posts: 49,998
    How about a headless 2nd table whose only row contains the totals? Place him directly underneath the main table, with no gaps. If done correctly, he'll appear to be the last row of the original table.

    Not only will he not move during sort, but also during scrolling (which I assume you'll also want).
  • 843806
    843806 Member Posts: 49,998
    This is not good, since if you change the column width, the bottom one will not change, of course you can set it's column width manually.
  • 843806
    843806 Member Posts: 49,998
    ice-space wrote:
    This is not good, since if you change the column width, the bottom one will not change,
    Just put a TableColumnModelListener on the TableColumnModel of the top table to keep the bottom table's column sizes in sync
    of course you can set it's column width manually.
    Manual effort isn't needed with the above listener implemented properly. Honestly, I haven't tried this myself, but it should be do-able.
  • 843806
    843806 Member Posts: 49,998
    ice-space wrote:
    This is not good, since if you change the column width, the bottom one will not change, of course you can set it's column width manually.
    Reply #6 is definitely the way to go. Very little code is needed and the same ColumnModelListener can be used on ALL columns.
  • 3004
    3004 Member Posts: 204,171
    Or just use the same column model in both tables.
This discussion has been closed.