I have a program that with Classpath incorrectly shows all columns having the same width. Our JTable can show the variable width columns, but it requires to call TableColumn.setSize(..) (ignoring the preferred size). Differently, the Sun's implementation ignores this and requires to call .setPreferredSize().

This patch makes the mentioned program to display the variable width columns under Classpath as well. I have also made the demo table columns variable width.

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

   * examples/gnu/classpath/examples/swing/TableDemo.java:
   Making the columns variable width.
   * javax/swing/JTable.java (distributeSpill, doLayout):
     Call getPreferredSize and not getSize().
Index: examples/gnu/classpath/examples/swing/TableDemo.java
===================================================================
RCS file: /sources/classpath/classpath/examples/gnu/classpath/examples/swing/TableDemo.java,v
retrieving revision 1.2
diff -u -r1.2 TableDemo.java
--- examples/gnu/classpath/examples/swing/TableDemo.java	27 Jan 2006 21:56:31 -0000	1.2
+++ examples/gnu/classpath/examples/swing/TableDemo.java	14 Feb 2006 22:17:26 -0000
@@ -45,7 +45,10 @@
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTable;
+import javax.swing.table.DefaultTableColumnModel;
 import javax.swing.table.DefaultTableModel;
+import javax.swing.table.TableColumn;
+import javax.swing.table.TableColumnModel;
 
 /**
  * Displays the editable table. The first column consists of check boxes.
@@ -62,7 +65,7 @@
   /**
    * The initial column count for this table.
    */ 
-  static int cols = 8;
+  static int cols = 7;
   
   
   /**
@@ -137,7 +140,7 @@
    * The table being displayed.
    */
   JTable table = new JTable();
-  
+ 
   /**
    * The table model.
    */
@@ -171,7 +174,6 @@
       {
         JPanel p = new JPanel();
         p.setLayout(new BorderLayout());
-        table.setModel(model);
         values = new Object[rows][];
         for (int i = 0; i < values.length; i++)
           {
@@ -183,6 +185,21 @@
             values [i][0] = i % 2 == 0? Boolean.TRUE : Boolean.FALSE;
           }
         
+        table.setModel(model);        
+        
+        // Make the columns with gradually increasing width:
+        DefaultTableColumnModel cm = new DefaultTableColumnModel();
+        for (int i = 0; i < cols; i++)
+          {
+            int w = 100+20*i;
+            TableColumn column = new TableColumn(i);
+            column.setPreferredWidth(w);
+            column.setHeaderValue("Width +"+(20*i));
+            cm.addColumn(column);            
+          }
+
+        table.setColumnModel(cm);
+
         // Create the table, place it into scroll pane and place
         // the pane into this frame.
         JScrollPane scroll = new JScrollPane();
@@ -209,4 +226,4 @@
     frame.validate();
     frame.setVisible(true);
   }
-}
\ No newline at end of file
+}
Index: javax/swing/JTable.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/JTable.java,v
retrieving revision 1.71
diff -u -r1.71 JTable.java
--- javax/swing/JTable.java	14 Feb 2006 19:53:23 -0000	1.71
+++ javax/swing/JTable.java	14 Feb 2006 22:17:55 -0000
@@ -3022,7 +3022,7 @@
     for (int i = 0; i < cols.length; i++)
       {
         if (cols[i] != null)
-          cols[i].setWidth(cols[i].getWidth() + average);
+          cols[i].setWidth(cols[i].getPreferredWidth() + average);
       }
   }
 
@@ -3044,7 +3044,7 @@
     for (int i = 0; i < ncols; ++i)
       {
         TableColumn col = columnModel.getColumn(i);
-        int p = col.getWidth();
+        int p = col.getPreferredWidth();
         pref[i] = p;
         prefSum += p;
         if (resizingColumn == col)

Reply via email to