?? tablecliper.java
字號:
package printtable;import java.awt.*;import java.awt.print.*;import java.util.*;import java.text.*;import javax.swing.*;import javax.swing.table.*;/** * Title: PrintTable * Description: A java jTable Print Programme. * Enable set the wighth and highth. * Copyright: Copyright (c) 2002 * Company: TopShine * @author ghostliang * @version 1.0 */public class TableCliper implements Printable,Pageable{ //variable for record the pageFormat private JTable table; private PageBorder head; private boolean showHead; private PageBorder foot; private boolean showFoot; private int headerStatus; private int tableAlignment; private int tableScale; private ExtPageFormat pageFormat; //init the class public TableCliper(JTable newTable,ExtPageFormat newPageFormat) { table = newTable; tableAlignment = newPageFormat.getTableAlignment(); headerStatus = newPageFormat.getHeaderType(); tableScale = newPageFormat.getTableScale(); head = newPageFormat.getHead(); foot = newPageFormat.getFoot(); showHead = newPageFormat.getShowHead(); showFoot = newPageFormat.getShowFoot(); pageFormat = newPageFormat; } //overwrite the print function of the print public int print(Graphics newGraphics,PageFormat newPageFormat,int index) { Dimension size = null; if((table.getWidth() == 0) || (table.getHeight() == 0)) { table.setSize(table.getPreferredSize()); } int tableWidth = table.getWidth(); int tableHeight = table.getHeight(); int positionX = 0; int positionY = 0; int pageIndex = 0; while(positionY < tableHeight) { positionX = 0; while(positionX < tableWidth) { size = getPrintSize(positionX,positionY); if(pageIndex == index) { paintTable(newGraphics,positionX,positionY,size); return Printable.PAGE_EXISTS; } pageIndex++; positionX += size.width; } positionY += size.height; } return Printable.NO_SUCH_PAGE; } //get how much area this page to print protected Dimension getPrintSize(int positionX,int positionY) { Rectangle rect; int printWidth; int printHeight; int firstCol = table.columnAtPoint(new Point(positionX,positionY)); int firstRow = table.columnAtPoint(new Point(positionX,positionY)); int maxWidth = (int)(pageFormat.getImageableWidth() * 100 / tableScale); int maxHeight = (int)(pageFormat.getImageableHeight() * 100 / tableScale); if(displayHeaderOnPage(positionY)) { maxHeight -= table.getTableHeader().getHeight(); } int lastCol = table.columnAtPoint(new Point(positionX + maxWidth,positionY)); if(lastCol == -1) printWidth = table.getWidth() - positionX; else { rect = table.getCellRect(0,lastCol - 1,true); printWidth = rect.x + rect.width - positionX; } int lastRow = table.rowAtPoint(new Point(positionX,positionY + maxHeight)); if(lastRow == -1) printHeight = table.getHeight() - positionY; else { rect = table.getCellRect(lastRow - 1,0,true); printHeight = rect.y + rect.height - positionY; } return new Dimension(printWidth,printHeight); } //show the table on the PrintComponent protected void paintTable(Graphics newGraphics,int positionX,int positionY, Dimension size) { Graphics2D newGraphics2D = (Graphics2D)newGraphics; int allOffsetX = 0; int allOffsetY = 0; //if head is exist,show it on the paper if(showHead) { int headWidth = (int)head.getWidth(); int headHeight = (int)head.getHeight(); int headImageableX = (int)head.getImageableX(); int headImageableY = (int)head.getImageableY(); int headImageableWidth = (int)head.getImageableWidth(); int headImageableHeight = (int)head.getImageableHeight(); newGraphics.setColor(Color.white); newGraphics.fillRect(0,0,headWidth,headHeight); //print the border of the head if(head.getBorderType() == 0) { newGraphics.setColor(Color.black); newGraphics.drawLine(headImageableX,headImageableY + headImageableHeight, headImageableWidth + headImageableX,headImageableY + headImageableHeight); } else if(head.getBorderType() == 1) { newGraphics.setColor(Color.black); newGraphics.drawRect(headImageableX,headImageableY, headImageableWidth,headImageableHeight); } //print string of head StringBuffer content; StringBuffer bakContent; Date localDate = new Date(); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); String localNowDate = sdf.format(localDate); if(head.leftContent != "") { content = new StringBuffer(head.leftContent); bakContent = new StringBuffer(head.leftContent); while( head.leftContent.indexOf('@') != -1 && head.leftContent.indexOf('@') != head.leftContent.length() - 1) { int flag = head.leftContent.indexOf('@'); switch(head.leftContent.charAt(flag + 1)) { case 'd': content.replace(flag,flag + 2,localNowDate); head.leftContent = content.toString(); break; case 'p': content.replace(flag,flag + 2, "" + this.getCurrentNumberOfPages(positionX,positionY)); head.leftContent = content.toString(); break; case 't': content.replace(flag,flag + 2, "" + this.getNumberOfPages()); head.leftContent = content.toString(); break; default: content.replace(flag,flag + 1, "#$"); head.leftContent = content.toString(); break; } } for(int index = 0;index < content.length() - 1;index++) if(content.charAt(index) == '#' && content.charAt(index + 1) == '$') content.replace(index,index + 2,"@"); head.leftContent = bakContent.toString(); newGraphics.setColor(Color.black); newGraphics.drawString(content.toString(),headImageableX + 10,headImageableY + (headImageableHeight / 2 + 6)); } if(head.midContent != "") { content = new StringBuffer(head.midContent); bakContent = new StringBuffer(head.midContent); while( head.midContent.indexOf('@') != -1 && head.midContent.indexOf('@') != head.midContent.length() - 1) { int flag = head.midContent.indexOf('@'); switch(head.midContent.charAt(flag + 1)) { case 'd': content.replace(flag,flag + 2,localNowDate); head.midContent = content.toString(); break; case 'p': content.replace(flag,flag + 2, "" + this.getCurrentNumberOfPages(positionX,positionY)); head.midContent = content.toString(); break; case 't': content.replace(flag,flag + 2, "" + this.getNumberOfPages()); head.midContent = content.toString(); break; default: content.replace(flag,flag + 1, "#$"); head.midContent = content.toString(); break; } } for(int index = 0;index < content.length() - 1;index++) if(content.charAt(index) == '#' && content.charAt(index + 1) == '$') content.replace(index,index + 2,"@"); head.midContent = bakContent.toString(); newGraphics.setColor(Color.black); newGraphics.drawString(content.toString(),headImageableX + (headImageableWidth / 2) - content.toString().length() * 4,headImageableY + (headImageableHeight / 2 + 6)); } if(head.rightContent != "") { content = new StringBuffer(head.rightContent); bakContent = new StringBuffer(head.rightContent); while( head.rightContent.indexOf('@') != -1 && head.rightContent.indexOf('@') != head.rightContent.length() - 1) { int flag = head.rightContent.indexOf('@'); switch(head.rightContent.charAt(flag + 1)) { case 'd': content.replace(flag,flag + 2,localNowDate); head.rightContent = content.toString(); break; case 'p': content.replace(flag,flag + 2, "" + this.getCurrentNumberOfPages(positionX,positionY)); head.rightContent = content.toString(); break; case 't': content.replace(flag,flag + 2, "" + this.getNumberOfPages()); head.rightContent = content.toString(); break; default: content.replace(flag,flag + 1, "#$"); head.rightContent = content.toString(); break; } } for(int index = 0;index < content.length() - 1;index++) if(content.charAt(index) == '#' && content.charAt(index + 1) == '$') content.replace(index,index + 2,"@"); head.rightContent = bakContent.toString(); newGraphics.setColor(Color.black); newGraphics.drawString(content.toString(),headImageableX + headImageableWidth - content.toString().length() * 8,headImageableY + (headImageableHeight / 2 + 6)); } newGraphics.translate(0,headHeight); allOffsetX += 0; allOffsetY += headHeight; } //print the main paper //a offset from the border of the paper int offsetX = (int)(pageFormat.getImageableX()); int offsetY = (int)(pageFormat.getImageableY()); //the size of the paper int paperWidth = (int)(pageFormat.getWidth()); int paperHeight = (int)(pageFormat.getHeight()); //draw main paper newGraphics.setColor(Color.white); newGraphics.fillRect(0,0,paperWidth,paperHeight); //save the clip Rectangle clipRect = newGraphics.getClipBounds(); //offset for alignment //size measured as table scale,so you must consider with table scale here if(tableAlignment == 1) newGraphics.translate((int)((pageFormat.getImageableWidth() - (double)(size.width * tableScale / 100))/2),0); else if(tableAlignment == 2) newGraphics.translate((int)(pageFormat.getImageableWidth() - (double)(size.width * tableScale / 100)),0);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -