aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes What listener for JTable header column size change Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "What listener for JTable header column size change" Watch "What listener for JTable header column size change" New topic
Author

What listener for JTable header column size change

Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
I have a JTable myTable.

How do I listen for when a user changes the size of a column (i.e. makes the column bigger using the column header)?

I assume that I would need to get the tables column model and use a listener from there but I cannot find what I need.

Thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
TableColumnModelListener()
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
Looking at the api TableColumnModelListener has the following:



void columnAdded(TableColumnModelEvent e)
Tells listeners that a column was added to the model.
void columnMarginChanged(ChangeEvent e)
Tells listeners that a column was moved due to a margin change.
void columnMoved(TableColumnModelEvent e)
Tells listeners that a column was repositioned.
void columnRemoved(TableColumnModelEvent e)
Tells listeners that a column was removed from the model.
void columnSelectionChanged(ListSelectionEvent e)
Tells listeners that the selection model of the TableColumnModel changed.


Not really sure any of those is what I am looking for. I need a columnResized() and I do not see that. As a test I implemented that interface and I do not receive any of those events when I drag a column in the header to make the column bigger/smaller.

Any ideas?
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Theodore David Williams wrote:
Not really sure any of those is what I am looking for. I need a columnResized() and I do not see that.

You could have simply implemented all five of those methods to print some debugging info to find the one does the thing you want.

I'll save you time, however, and tell you that columnMarginChanged() is the one you want.


bitguru blog
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
Thanks , a few more questions.

1. When I get that event is there a way that I can extract which column width changed, or do I basically assume all columns changed? I.E. when I get source on the event it is DefaultTableColumnModel and that is for all the columns not just the one that changed.

2. I am trying to save the users column information as preferences so that when they launch again they will not have to resize their columns. The problem is listening for columnMarginChanged() and saving the column width preferences does not really work. The preferences get saved based on column width every time the user drags the mouse to re-size the column so that part is fine. However when the user exits the gui and comes back in the table is rebuilt and I get the columnMarginChanged() event with default (not my saved) column widths, when this happens I save over my old column widths with the new default ones. Again because the columnMarginChanged() is being called when the user resizes the columns AND when the gui is initialized with default column widths. Make sense but I am not sure how to get around this. Any ideas???
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

Theodore David Williams wrote:1. When I get that event is there a way that I can extract which column width changed, or do I basically assume all columns changed? I.E. when I get source on the event it is DefaultTableColumnModel and that is for all the columns not just the one that changed.

I just looked at the API documentation. The parameter for the ColumnMarginChanged method is a ChangeEvent, and as you can see from the documentation for that class, it doesn't contain any useful information. So you have to go and look at the column model to see what changed.
2. I am trying to save the users column information as preferences so that when they launch again they will not have to resize their columns. The problem is listening for columnMarginChanged() and saving the column width preferences does not really work. The preferences get saved based on column width every time the user drags the mouse to re-size the column so that part is fine. However when the user exits the gui and comes back in the table is rebuilt and I get the columnMarginChanged() event with default (not my saved) column widths, when this happens I save over my old column widths with the new default ones. Again because the columnMarginChanged() is being called when the user resizes the columns AND when the gui is initialized with default column widths. Make sense but I am not sure how to get around this. Any ideas???

You want to treat the changes when the GUI is being constructed differently from the changes when the user is doing things to the GUI? Then sure, do that. Have a boolean flag which is true when the GUI is being constructed and false otherwise, and consult that flag. Or don't add the listener until after the GUI is constructed.
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102

I just looked at the API documentation. The parameter for the ColumnMarginChanged method is a ChangeEvent, and as you can see from the documentation for that class, it doesn't contain any useful information. So you have to go and look at the column model to see what changed.


Yeah I see that. My point is

I.E. when I get source on the event it is a DefaultTableColumnModel and that is for all the columns not just the one that changed.


TableColumnModel is for all columns right? What I really want is 'give me the one column that was just changed', i.e. which column changed that fired this event. Either that or go column by column to see what changed by I think you need to keep your own history for this, i.e. there is no TableColumn.didYouJustChange() method.

Thanks again for the help!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

I suspect it's because when you move the border between two columns, there are two columns whose margins have been moved.
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102

I suspect it's because when you move the border between two columns, there are two columns whose margins have been moved.


Yeah I think you are correct. In my case however my table is scrollable and when you change one column to be bigger/smaller the scroll bar accommodates for the change such that none of the other columns need to change size. Regardless it would be nice to listen for an event on the column itself, i.e. if two or three columns change then I would get two or three events, one for each column that changed. Is there such an event I can listen for?
Ally Cavs
Ranch Hand

Joined: Aug 25, 2008
Posts: 88

2. I am trying to save the users column information as preferences so that when they launch again they will not have to resize their columns. The problem is listening for columnMarginChanged() and saving the column width preferences does not really work


Guys its probably too late now considering the thread is a few years old, but i was doing this and using boolean flags to distinguish between gui initializing and user changes. Of course after getting so far I had tunnel vision. But the the answer is to save the settings when the applicatoin is been closed down by the user. works perfect
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

I don't see how mentioning saving settings adds anything new to the discussion in this thread, as using Preferences has already been talked about.

But in case anyone else has the requirement of monitoring changes in a table's column width, the solution is to add a PropertyChangeListener("width") to the TableColumn.


luck, db
There are no new questions, but there may be new answers.
Ally Cavs
Ranch Hand

Joined: Aug 25, 2008
Posts: 88
Hey Daryl...


2. I am trying to save the users column information as preferences so that when they launch again they will not have to resize their columns. The problem is listening for columnMarginChanged() and saving the column width preferences does not really work. The preferences get saved based on column width every time the user drags the mouse to re-size the column so that part is fine. However when the user exits the gui and comes back in the table is rebuilt and I get the columnMarginChanged() event with default (not my saved) column widths


preferences are been saved with defaults on gui initiation when restarting the app cause columnMarginChanged is been called. i had this issue too. best solution is saving on closing the app.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What listener for JTable header column size change
 
Similar Threads
JTable
JTable
JTable Header column listener
Row Selection problems with JTable
JTable