Search code examples
javaswingjtablelistener

Java JTable detect Column re-sized by user


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.


Solution

  • 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

    class MyTable extends JTable {
    
        private boolean isColumnWidthChanged;
        public boolean getColumnWidthChanged() {
            return isColumnWidthChanged;
        }
    
        public void setColumnWidthChanged(boolean widthChanged) {
            isColumnWidthChanged = widthChanged;
        }
    
    }
    

    Step 2 - Add a TableColumnModelListener() to the table

    private class TableColumnWidthListener implements TableColumnModelListener
    {
        @Override
        public void columnMarginChanged(ChangeEvent e)
        {
            /* columnMarginChanged is called continuously as the column width is changed
               by dragging. Therefore, execute code below ONLY if we are not already
               aware of the column width having changed */
            if(!tableObj.hasColumnWidthChanged())
            {
                /* the condition  below will NOT be true if
                   the column width is being changed by code. */
                if(tableObj.getTableHeader.getResizingColumn() != null)
                {
                    // User must have dragged column and changed width
                    tableObj.setColumnWidthChanged(true);
                }
            }
        }
    
        @Override
        public void columnMoved(TableColumnModelEvent e) { }
    
        @Override
        public void columnAdded(TableColumnModelEvent e) { }
    
        @Override
        public void columnRemoved(TableColumnModelEvent e) { }
    
        @Override
        public void columnSelectionChanged(ListSelectionEvent e) { }
    }
    

    Step 3 - Add a mouse listener to the table header

    private class TableHeaderMouseListener extends MouseAdapter
    {
        @Override
        public void mouseReleased(MouseEvent e)
        {
            /* On mouse release, check if column width has changed */
            if(tableObj.getColumnWidthChanged())
            {
                // Do whatever you need to do here
    
                // Reset the flag on the table.
                tableObj.setColumnWidthChanged(false);
            }
        }
    }
    

    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!