Exploring various variants of tables, I discovered that the columns in tables with default table model are still not properly resized. This is because the columns in such tables have the preferredWidth property change listeners installed. This patch disables such listeners for the duration of the resizing action. Listeners should probably be installed in the advanced tables with the custom table models as well.

2006-02-16  Audrius Meskauskas  <[EMAIL PROTECTED]>

   * javax/swing/JTable.java
   (TableColumnPropertyChangeHandler.propertyChange): Return without
   action if table header resizing column in not null. (doLayout):
Only repaint the header if it is not null. * javax/swing/plaf/basic/BasicTableHeaderUI.java
   (MouseInputHandler.mouseExited, MouseInputHandler.mouseReleased):
   Rewritten. (MouseInputHandler.endResizing): New method.
Index: javax/swing/JTable.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/JTable.java,v
retrieving revision 1.77
diff -u -r1.77 JTable.java
--- javax/swing/JTable.java	16 Feb 2006 14:30:56 -0000	1.77
+++ javax/swing/JTable.java	16 Feb 2006 19:39:23 -0000
@@ -985,9 +985,9 @@
   class TableColumnPropertyChangeHandler implements PropertyChangeListener
   {
     /**
-     * Receives notification that a property of the observed TableColumns
-     * has changed.
-     *
+     * Receives notification that a property of the observed TableColumns has
+     * changed.
+     * 
      * @param ev the property change event
      */
     public void propertyChange(PropertyChangeEvent ev)
@@ -995,13 +995,15 @@
       if (ev.getPropertyName().equals("preferredWidth"))
         {
           JTableHeader header = getTableHeader();
-	  if (header != null)
-	    {
-	      TableColumn col = (TableColumn) ev.getSource();
-	      header.setResizingColumn(col);
-	      doLayout();
-	      header.setResizingColumn(null);
-	    }
+          if (header != null)
+            // Do nothing if the table is in the resizing mode.
+            if (header.getResizingColumn() == null)
+              {
+                TableColumn col = (TableColumn) ev.getSource();
+                header.setResizingColumn(col);
+                doLayout();
+                header.setResizingColumn(null);
+              }
         }
     }
   }
@@ -3171,7 +3173,8 @@
     // editing is started immediately after the program start or cell
     // resizing. 
     repaint();
-    getTableHeader().repaint();
+    if (tableHeader!=null)
+      tableHeader.repaint();
   }
   
   /**
Index: javax/swing/plaf/basic/BasicTableHeaderUI.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/plaf/basic/BasicTableHeaderUI.java,v
retrieving revision 1.15
diff -u -r1.15 BasicTableHeaderUI.java
--- javax/swing/plaf/basic/BasicTableHeaderUI.java	16 Feb 2006 14:30:55 -0000	1.15
+++ javax/swing/plaf/basic/BasicTableHeaderUI.java	16 Feb 2006 19:39:25 -0000
@@ -175,10 +175,8 @@
      */
     public void mouseExited(MouseEvent e)
     {
-      header.setResizingColumn(null);
-      showingResizeCursor = false;
-      if (timer!=null)
-        timer.stop();
+      if (header.getResizingColumn()!=null)
+        endResizing();
     }
 
     /**
@@ -258,7 +256,16 @@
      */
     public void mouseReleased(MouseEvent e)
     {
-      TableColumnModel model = header.getColumnModel();
+      if (header.getResizingColumn()!=null)
+        endResizing();
+    }
+    
+    /**
+     * Stop resizing session.
+     */
+    void endResizing()
+    {
+      TableColumnModel model = header.getColumnModel();     
       int n = model.getColumnCount();
       if (n>2)
         {
@@ -269,6 +276,11 @@
               c.setPreferredWidth(c.getWidth());
             }
         }
+      header.setResizingColumn(null);
+      showingResizeCursor = false;
+      if (timer!=null)
+        timer.stop();
+      header.setCursor(Cursor.getDefaultCursor());
     }
   }
  

Reply via email to