For those of us who don't know Excel that well, this option allows the user to have the top X columns (X is configurable) remain fixed. Scrolling will be limited to the remaining columns. The same can be applied to the left X rows as well. If both a number of rows and a number of columns are frozen the bottom-right part contains the scrolling.
I can think of one technique to do this:
- create one JTable with the TableModel. Use a RowSorter with a RowFilter to filter out all rows with a row number >= X. This will be the table part with the header and the first X rows.
- create another JTable with the same TableModel. Again, use a RowSorter with a RowFilter to filter out all rows with a row number < X. This will be the table part with everything but the header and the first X rows.
I haven't tested this, but I think this should do it. You basically moved X rows from the JScrollPane's view port to its column header.
Edit: scratch step 3. Instead of using two TableColumnModels that are synchronized using listeners, just set the bottom JTable's TableColumnModel to that of the top JTable.
Here's that all written in code, using the sharing of the TableColumnModel:
This has two problems:
1) the JScrollPane's column header will be reset by the JScrollPane automatically to the JTableHeader of the second table.
2) the resizing of columns is now somehow broken; dragging works as expected.
I'm sure one of our Swing guru's (Rob C, Darryl B, Pete S, Michael D) can help fix these issues.
That code works for freezing the left X columns. Now do the same for freezing the top X rows
The problem with that is the table header. You'll need to show only one (or two, if you also freeze the left X columns), but the resizing / reordering will need to work on the bottom table(s) as well. Still, by not using JScrollPane's column header, row header and other non-viewport components the solution may solve the problems I had with my code.
Yes, in the past I've tried to share the TableColumnModel between multple tables (when I was trying to create a "footer" row) and have had the same problems so I don't have anything new to offer.
I also originally read the question the same way Michael did, but since the OP accepted your response I didn't bother posting my standard solution which is to check out the Fixed Column Table
my attempt (limited testing, so probably very brittle)
One problem is using the keyboard for navigation. The row selection isn't in sync. My solution above shares the ListSelectionModel, which works for both mouse and keyboard navigation/selection.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.