I improved the painting of JTables. Before we always painted the whole
table, regardless of how much really needs painting. Imagine that with a
huge table like the one in our TableDemo. Now we only paint the table
cells that are actually in the clip and now can even scroll 1000x1000
quite efficiently (remember that the viewport blits most of the table
while scrolling).

Also I made the paint method make use of JTable.getCellRect() which makes
painting more consistent with JTableHeader.

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

        * javax/swing/plaf/basic/BasicTableUI.java
        (paint): Determine the cells that need painting based on the
        current clip. Use getCellRect() for calculating the cell
        bounds.

/Roman
Index: javax/swing/plaf/basic/BasicTableUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTableUI.java,v
retrieving revision 1.40
diff -u -r1.40 BasicTableUI.java
--- javax/swing/plaf/basic/BasicTableUI.java	19 Jan 2006 12:40:52 -0000	1.40
+++ javax/swing/plaf/basic/BasicTableUI.java	14 Feb 2006 15:20:08 -0000
@@ -1230,58 +1230,35 @@
       return;
 
     Rectangle clip = gfx.getClipBounds();
-    TableColumnModel cols = table.getColumnModel();
 
-    int height = table.getRowHeight() + table.getRowMargin();
-    int x0 = 0, y0 = 0;
-    int x = x0;
-    int y = y0;
-
-    Dimension gap = table.getIntercellSpacing();
-    int ymax = clip.y + clip.height;
-    int xmax = clip.x + clip.width;
-    Rectangle bounds = new Rectangle();
-    
+    Point p1 = new Point(clip.x, clip.y);
+    int c0 = table.columnAtPoint(p1);
+    int r0 = table.rowAtPoint(p1);
+    Point p2 = new Point(clip.x + clip.width, clip.y + clip.height);
+    int cn = table.columnAtPoint(p2);
+    int rn = table.rowAtPoint(p2);
+    Rectangle bounds;
     // paint the cell contents
-    for (int c = 0; c < ncols && x < xmax; ++c)
+    Color grid = table.getGridColor();    
+    for (int c = c0; c <= cn; ++c)
       {
-        y = y0;
-        TableColumn col = cols.getColumn(c);
-        int width = col.getWidth();
-        int halfGapWidth = gap.width / 2;
-        int halfGapHeight = gap.height / 2;
-        
-        for (int r = 0; r < nrows && y < ymax; ++r)
+        for (int r = r0; r <= rn; ++r)
           {
-            bounds.x = x + halfGapWidth;
-            bounds.y = y + halfGapHeight + 1;
-            bounds.width = width - gap.width + 1;
-            bounds.height = height - gap.height;
-            if (bounds.intersects(clip))
-              {           
-                paintCell(gfx, r, c, bounds, table.getCellRenderer(r, c));
-              }
-            y += height;
+            bounds = table.getCellRect(r, c, false);
+            paintCell(gfx, r, c, bounds, table.getCellRenderer(r, c));
           }
-        x += width;
       }
 
-    // tighten up the x and y max bounds
-    ymax = y;
-    xmax = x;
-
-    Color grid = table.getGridColor();    
-
     // paint vertical grid lines
     if (grid != null && table.getShowVerticalLines())
       {    
-        x = x0;
         Color save = gfx.getColor();
         gfx.setColor(grid);
-        for (int c = 0; c < ncols && x < xmax; ++c)
+        for (int c = c0; c < cn; ++c)
           {
-            x += cols.getColumn(c).getWidth();
-            gfx.drawLine(x, y0, x, ymax);
+            bounds = table.getCellRect(0, c, true);
+            gfx.drawLine(bounds.x + bounds.width, p1.y,
+                         bounds.x + bounds.width, p2.y);
           }
         gfx.setColor(save);
       }
@@ -1289,13 +1266,13 @@
     // paint horizontal grid lines    
     if (grid != null && table.getShowHorizontalLines())
       {    
-        y = y0;
         Color save = gfx.getColor();
         gfx.setColor(grid);
-        for (int r = 0; r < nrows && y < ymax; ++r)
+        for (int r = r0; r < rn; ++r)
           {
-            y += height;
-            gfx.drawLine(x0, y, xmax, y);
+            bounds = table.getCellRect(r, 0, true);
+            gfx.drawLine(p1.x, bounds.y + bounds.height,
+                         p2.x, bounds.y + bounds.height);
           }
         gfx.setColor(save);
       }

Reply via email to