Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

changing column width in linked JTables  RSS feed

 
Steven Gollery
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to put put together a window that has (among other things) two JTables that are linked together for horizontal scrolling (don't ask why -- it's just one of those requirements). The scrolling itself works fine, but then I have to deal with what happens when a user makes changes to the columns in one table. I need to be able to duplicate the changes in the other table.

The first problem was changing the order of columns, so I extended JTable and overrode columnMoved, and everything works fine.

But now I come to the issue of the user changing the width of a column in one table and wanting the same column in the other table to have the same width. Overriding columnMarginChanged seems like it should be the way to go, but the ChangeEvent doesn't give me any usable information, at least as far as I can tell.

I'm sure this problem has been solved before and I just haven't been searching on the right topics, so if anybody can point me in the right direction here, it would be a big help.

Thanks in advance,

Steve Gollery
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Gollery:
I have to deal with what happens when a user makes changes to the columns in one table. I need to be able to duplicate the changes in the other table.

The first problem was changing the order of columns, [...]

But now I come to the issue of the user changing the width of a column in one table and wanting the same column in the other table to have the same width.


This can be pretty simple, actually, unless you need the column names to differ. (This approach handles column reordering, too.)


JTable table1 = new JTable(7, 12);
JTable table2 = new JTable(11, 12);

table2.setColumnModel(table1.getColumnModel());

table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

JPanel p = new JPanel(new GridLayout(1, 0, 20, 0));
p.add(new JScrollPane(table1));
p.add(new JScrollPane(table2));

[edit: Sometimes bold text works inside code blocks, sometimes it doesn't. It didn't this time so I removed the code block tags.]
[ July 13, 2007: Message edited by: Brian Cole ]
 
Steven Gollery
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,

Thanks. That works perfectly.

Steve
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!