• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing JTable  RSS feed

 
Ida Achi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I would like to make 2 JTables out of one JTable. I mean like splitting a JTable in the middle into 2 JTables. The renderer class, Table model class and all the column heads remain the same. The only difference is JTable 1 should contain A-M and JTable 2 should contain N-Z fields. Is there a way do do that in Swing?
Thank you.
 
Rob Spoor
Sheriff
Posts: 21052
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to do some copy work (see step 2), but overall it should be possible.

1) create two JTables that both share the same TableModel. From JTable 1, remove all columns from N-Z, from JTable 2, remove all columns from A-M.
This will remove the columns from the JTable only; they will still be part of the TableModel. For the second JTable it will be similar, just adjust the loop index (start at 12, go to 0)

2) set the cell renderers / editors and all other direct changes (listeners etc) to both JTables

3) create two JScrollPanes that each contain one of the two JTables. Get the vertical JScrollBars from both, and set the model of the second JScrollBar to be that of the first one:
This will ensure that scrolling vertically in one of the two JTables will make the other one scroll the same.

4) add both JScrollPanes to your parent container.
 
Ida Achi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot.
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Table model class and all the column heads remain the same


Sounds like you want to split the "rows", not the "columns" as suggested above.

If this is true then you can use a table row sorter and filter the rows. Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" where you will find a working example on using filtering.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!