First JTable Column Always Visible Despite Horizontal Scrolling
796262Oct 22 2010 — edited Oct 25 2010Hey friends,
A coworker of mine came to me yesterday with a requirement. I wanted to give you the solution we came up with and see if you guys saw any obvious problems with it or have any other ideas.
The requirement: We have a JTable that has a bunch of columns, which activates horizontal scrolling. However, we want the first column of the JTable to remain visible, no matter what the horizontal scrolling.
The solution: Use two JTables displayed right next to one another, one consisting of the first column, the other consisting of the rest of the columns. I see three problems with this, which I think we've solved:
Selection: Selecting a row in one of the JTables should affect the other, to keep up the illusion that it's actually one JTable. To remedy this, use a selection listener on each JTable and make the appropriate changes to the other upon a SelectionEvent (I'm not looking up what the actual listener and event names are, but you know what I mean).
Vertical scrolling: Same idea as above. Use a scroll listener on the vertical scroll bar of the JTable containing the rest of the columns (the JTable with only the first column won't display its vertical scrollbar), then scroll the single-column JTable to the visible rect, adjusting for any horizontal scrolling.
Sorting: Sorting of either JTable should also sort the other by the sorted row. To do this, each table's model has to include the other's data, which can be sorted on when the other JTable is sorted.
Am I missing any other problems with the solution? Or is there simpler way to solve the requirement? Sorry I don't have any code or an SSCCE to show you, but I'm hoping to get a high level once-over sanity check.