Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 947
Dead_Jester Avatar asked Jan 06 '12 02:01

Dead_Jester


2 Answers

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!

like image 107
eternaln00b Avatar answered Sep 19 '22 14:09

eternaln00b


A simpler solution might just be to listen on the mouse release event (which happens only once in this user interaction) and check if the column-sizes changed in the meantime ? I'm using the code below to listen for column reordering and size changes.

getTableHeader().addMouseListener( new MouseAdapter() {
    public void mouseReleased(MouseEvent arg0)
    {
      String colNamesAndSizes = "";
      for( int i=0;i<getColumnModel().getColumnCount();i++ ) {

        if( i>0 ) colNamesAndSizes += ",";
        TableColumn column = getColumnModel().getColumn(i);
        colNamesAndSizes += column.getHeaderValue();
        colNamesAndSizes += ":";
        colNamesAndSizes += column.getWidth();
      }
      // check if changed, if yes, persist...
   }});
like image 21
Axel Podehl Avatar answered Sep 17 '22 14:09

Axel Podehl