< prev index next >
src/java.desktop/share/classes/javax/swing/TablePrintable.java
Print this page
@@ -203,15 +203,13 @@
* NO_SUCH_PAGE if a non-existent page index is specified
* @throws PrinterException if an error causes printing to be aborted
*/
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
-
// for easy access to these values
final int imgWidth = (int)pageFormat.getImageableWidth();
final int imgHeight = (int)pageFormat.getImageableHeight();
-
if (imgWidth <= 0) {
throw new PrinterException("Width of printable area is too small.");
}
// to pass the page number when formatting the header and footer text
@@ -300,14 +298,16 @@
// rather than multiplying every row and column by the scale factor
// in findNextClip, just pass a width and height that have already
// been divided by it
int scaledWidth = (int)(imgWidth / sf);
int scaledHeight = (int)((availableSpace - hclip.height) / sf);
-
// calculate the area of the table to be printed for this page
findNextClip(scaledWidth, scaledHeight);
+ if (!((table.getBounds()).intersects(clip))) {
+ return NO_SUCH_PAGE;
+ }
last++;
}
// create a copy of the graphics so we don't affect the one given to us
Graphics2D g2d = (Graphics2D)graphics.create();
@@ -341,11 +341,10 @@
tempRect.x = 0;
tempRect.y = 0;
tempRect.width = imgWidth;
tempRect.height = availableSpace;
g2d.clip(tempRect);
-
// if we have a scale factor, scale the graphics object to fit
// the entire width
if (sf != 1.0D) {
g2d.scale(sf, sf);
@@ -387,11 +386,30 @@
g2d.setTransform(oldTrans);
g2d.setClip(oldClip);
// draw a box around the table
g2d.setColor(Color.BLACK);
- g2d.drawRect(0, 0, clip.width, hclip.height + clip.height);
+
+ // compute the visible portion of table and draw the rect around it
+ Rectangle visibleBounds = clip.intersection(table.getBounds());
+ Point upperLeft = visibleBounds.getLocation();
+ Point lowerRight = new Point(visibleBounds.x + visibleBounds.width,
+ visibleBounds.y + visibleBounds.height);
+
+ int rMin = table.rowAtPoint(upperLeft);
+ int rMax = table.rowAtPoint(lowerRight);
+ if (rMin == -1) {
+ rMin = 0;
+ }
+ if (rMax == -1) {
+ rMax = table.getRowCount();
+ }
+ int rowHeight = 0;
+ for(int visrow = rMin; visrow < rMax; visrow++) {
+ rowHeight += table.getRowHeight(visrow);
+ }
+ g2d.drawRect(0, 0, visibleBounds.width, hclip.height + rowHeight);
// dispose the graphics copy
g2d.dispose();
return PAGE_EXISTS;
@@ -507,11 +525,10 @@
}
if (++col >= colCount) {
// reset col to 0 to indicate we're finished all columns
col = 0;
-
break;
}
colWidth = colModel.getColumn(col).getWidth();
} while (clip.width + colWidth <= pw);
< prev index next >