This patch implements the getMinimumSize and getMaximumSize methods in
BasicTableUI, essentially fixing bug PR swing/23204 where table columns
weren't being automatically resized.  I have not yet closed this bug
because changes need to be made (most likely in ViewportLayout) to allow
JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF) to disable column
autoresizing.  Once this is done I will consider this bug fixed and
close it.

The patch is attached.

2005-08-31  Anthony Balkissoon  <[EMAIL PROTECTED]>

        * javax/swing/JTable.java:
        (initializeLocalVars): Changed default autoResizeMode to 
        AUTO_RESIZE_SUBSEQUENT_COLUMNS to match the JDK.
        * javax/swing/plaf/basic/BasicTableUI.java:
        (getMaximumSize): Implemented.
        (getMinimumSize): Implemented.

--Tony
Index: javax/swing/JTable.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JTable.java,v
retrieving revision 1.43
diff -u -r1.43 JTable.java
--- javax/swing/JTable.java	12 Aug 2005 20:45:17 -0000	1.43
+++ javax/swing/JTable.java	31 Aug 2005 18:52:08 -0000
@@ -673,7 +673,7 @@
     this.defaultEditorsByColumnClass = new Hashtable();
     createDefaultEditors();
 
-    this.autoResizeMode = AUTO_RESIZE_ALL_COLUMNS;
+    this.autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS;
     this.rowHeight = 16;
     this.rowMargin = 1;
     this.rowSelectionAllowed = true;
Index: javax/swing/plaf/basic/BasicTableUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTableUI.java,v
retrieving revision 1.24
diff -u -r1.24 BasicTableUI.java
--- javax/swing/plaf/basic/BasicTableUI.java	16 Aug 2005 18:07:50 -0000	1.24
+++ javax/swing/plaf/basic/BasicTableUI.java	31 Aug 2005 18:52:08 -0000
@@ -206,14 +206,44 @@
     return new MouseInputHandler();
   }
 
+  /**
+   * Return the maximum size of the table. The maximum height is the row 
+    * height times the number of rows. The maximum width is the sum of 
+    * the maximum widths of each column.
+    * 
+    *  @param comp the component whose maximum size is being queried,
+    *  this is ignored.
+    *  @return a Dimension object representing the maximum size of the table,
+    *  or null if the table has no elements.
+   */
   public Dimension getMaximumSize(JComponent comp) 
   {
-    return getPreferredSize(comp);
+    int maxTotalColumnWidth = 0;
+    for (int i = 0; i < table.getColumnCount(); i++)
+      maxTotalColumnWidth += table.getColumnModel().getColumn(i).getMaxWidth();
+    if (maxTotalColumnWidth == 0 || table.getRowCount() == 0)
+      return null;
+    return new Dimension(maxTotalColumnWidth, table.getRowCount()*table.getRowHeight());
   }
 
+  /**
+   * Return the minimum size of the table. The minimum height is the row 
+    * height times the number of rows. The minimum width is the sum of 
+    * the minimum widths of each column.
+    * 
+    *  @param comp the component whose minimum size is being queried,
+    *  this is ignored.
+    *  @return a Dimension object representing the minimum size of the table,
+    *  or null if the table has no elements.
+   */
   public Dimension getMinimumSize(JComponent comp) 
   {
-    return getPreferredSize(comp);
+    int minTotalColumnWidth = 0;
+    for (int i = 0; i < table.getColumnCount(); i++)
+      minTotalColumnWidth += table.getColumnModel().getColumn(i).getMinWidth();
+    if (minTotalColumnWidth == 0 || table.getRowCount() == 0)
+      return null;
+    return new Dimension(minTotalColumnWidth, table.getRowCount()*table.getRowHeight());
   }
 
   public Dimension getPreferredSize(JComponent comp) 
@@ -355,7 +385,7 @@
     {
       ListSelectionModel rowModel = table.getSelectionModel();
       ListSelectionModel colModel = table.getColumnModel().getSelectionModel();
-      
+
       int rowLead = rowModel.getLeadSelectionIndex();
       int rowMax = table.getModel().getRowCount() - 1;
       
_______________________________________________
Classpath-patches mailing list
Classpath-patches@gnu.org
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to