I have a JTable that is using a TableColumnModelListener() to detect when the column has been re-sized and I have some code I want to execute in the columnMarginChanged() method.
How do I determine whether the column was re-sized by the user or as a result of other code?
I am thinking I have to start with ChangeEvent.getSource() but I don’t know where to go from there.
Thank you.
I can give you one possible approach. I was trying to solve the same problem, because I wanted to serialize information about column widths to disk, so that the next time the table opened up in my application, I could restore the column widths appropriately. Here goes:
Step 1 – Override your JTable and add a boolean property to it
Step 2 – Add a TableColumnModelListener() to the table
Step 3 – Add a mouse listener to the table header
NOTE: In my application, the TableHeaderMouseListener and TableColumnWidthListener classes were private inner classes of my main application class. My main application class held on to a reference of the table being observed. Therefore, these inner classes had access to the table instance. Obviously, depending on your setup, you need to do the appropriate thing to make the table instance available to these other classes. Hope this helps!