This fixes the calculation of the repaint rectangle when column
selection changes for some special cases.

2006-06-02  Roman Kennke <[EMAIL PROTECTED]>

        * javax/swing/JTable.java
        (columnSelectionChanged): Don't return when there's only one
        column (might still need repainting). Correctly calculate
        repaint rectangle.
        (valueChanged): Use return value of SwingUtilities.computeUnion
        as dirty region.

/Roman

Index: javax/swing/JTable.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JTable.java,v
retrieving revision 1.113
diff -u -1 -0 -r1.113 JTable.java
--- javax/swing/JTable.java	1 Jun 2006 14:17:20 -0000	1.113
+++ javax/swing/JTable.java	2 Jun 2006 13:30:17 -0000
@@ -2774,44 +2774,60 @@
     repaint();
   }
   
   /**
    * Invoked when the the column selection changes, repaints the changed
    * columns. It is not recommended to override this method, register the
    * listener instead.
    */
   public void columnSelectionChanged (ListSelectionEvent event)
   {
-    // Does not make sense for the table with the single column.
-    if (getColumnCount() < 2)
-      return;
-    
-    int x0 = 0;
-    
     // We must limit the indices to the bounds of the JTable's model, because
     // we might get values of -1 or greater then columnCount in the case
     // when columns get removed.
     int idx0 = Math.max(0, Math.min(getColumnCount() - 1,
                                     event.getFirstIndex()));
     int idxn = Math.max(0, Math.min(getColumnCount() - 1,
                                     event.getLastIndex()));
-    int i;
 
-    for (i = 0; i < idx0; i++)
-      x0 += columnModel.getColumn(i).getWidth();
-    
-    int xn = x0;
-    
-    for (i = idx0; i <= idxn; i++)
-      xn += columnModel.getColumn(i).getWidth();
-    
-    repaint(x0, 0, xn-x0, getHeight());
+    int minRow = 0;
+    int maxRow = getRowCount() - 1;
+    if (getRowSelectionAllowed())
+      {
+        minRow = selectionModel.getMinSelectionIndex();
+        maxRow = selectionModel.getMaxSelectionIndex();
+        int leadRow = selectionModel.getLeadSelectionIndex();
+        if (minRow == -1 && maxRow == -1)
+          {
+            minRow = leadRow;
+            maxRow = leadRow;
+          }
+        else
+          {
+            // In this case we need to repaint also the range to leadRow, not
+            // only between min and max.
+            if (leadRow != -1)
+              {
+                minRow = Math.min(minRow, leadRow);
+                maxRow = Math.max(maxRow, leadRow);
+              }
+          }
+      }
+    if (minRow != -1 && maxRow != -1)
+      {
+        Rectangle first = getCellRect(minRow, idx0, false);
+        Rectangle last = getCellRect(maxRow, idxn, false);
+        Rectangle dirty = SwingUtilities.computeUnion(first.x, first.y,
+                                                      first.width,
+                                                      first.height, last);
+        repaint(dirty);
+      }
   }
  
   /**
    * Invoked when the editing is cancelled.
    */
   public void editingCanceled (ChangeEvent event)
   {
     if (editorComp!=null)
       {
         remove(editorComp);
@@ -2906,23 +2922,24 @@
   {
     // If we are in the editing process, end the editing session.
     if (isEditing())
       editingStopped(null);
     
     // Repaint the changed region.
     int first = Math.max(0, Math.min(getRowCount() - 1, event.getFirstIndex()));
     int last = Math.max(0, Math.min(getRowCount() - 1, event.getLastIndex()));
     Rectangle rect1 = getCellRect(first, 0, false);
     Rectangle rect2 = getCellRect(last, getColumnCount() - 1, false);
-    SwingUtilities.computeUnion(rect2.x, rect2.y, rect2.width, rect2.height,
-                                rect1);
-    repaint(rect1);
+    Rectangle dirty = SwingUtilities.computeUnion(rect2.x, rect2.y,
+                                                  rect2.width, rect2.height,
+                                                  rect1);
+    repaint(dirty);
   }
 
  /**
    * Returns index of the column that contains specified point 
    * or -1 if this table doesn't contain this point.
    *
    * @param point point to identify the column
    * @return index of the column that contains specified point or 
    * -1 if this table doesn't contain this point.
    */

Reply via email to