DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 1 of 1
  1. #1
    Join Date
    May 2006
    Posts
    15

    Swing:How to include Title in printed page in swing

    Hello...i'm doing some swing application...the functions including to print lists of programs in a table....so rite now i can print those lists with page number ...but my boss wants to include Title in each page...how can i include the title
    This are the code to print table....can anyone give any idea

    Code:
    private void btnPrintActionPerformed(ActionEvent evt){ 
            System.out.println("action");
            collapseTableData();
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(this);
            printJob.printDialog();
            try { 	
            	printJob.print();
            } catch (Exception PrintException) { }
        }
    
        public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
         	Graphics2D  g2 = (Graphics2D) g;
         	g2.setColor(Color.black);
         	int fontHeight=g2.getFontMetrics().getHeight();
         	int fontDesent=g2.getFontMetrics().getDescent();
    
         	//leave room for page number
         	double pageHeight = pageFormat.getImageableHeight()-fontHeight;
         	double pageWidth = pageFormat.getImageableWidth();
         	double tableWidth = (double)tblProgram.getColumnModel().getTotalColumnWidth();
         	double scale = 1; 
         	if (tableWidth >= pageWidth) {
         		scale =  pageWidth / tableWidth;
         	}
         	double headerHeightOnPage =	tblProgram.getTableHeader().getHeight()*scale;
         	double tableWidthOnPage = tableWidth*scale;
         	double oneRowHeight = (tblProgram.getRowHeight()+	tblProgram.getRowMargin())*scale;
         	int numRowsOnAPage = (int)((pageHeight-headerHeightOnPage)/oneRowHeight);
         	double pageHeightForTable = oneRowHeight * numRowsOnAPage;
         	int totalNumPages = (int)Math.ceil(((double)tblProgram.getRowCount()) / numRowsOnAPage);
         	if(pageIndex>=totalNumPages) {
         		return NO_SUCH_PAGE;
         	}
         	g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    
         	//bottom center
         	g2.drawString("Page: " +(pageIndex+1), (int)pageWidth/2-35, (int)(pageHeight
         	    		+fontHeight-fontDesent));
    
         	g2.drawString("Ilmu Tools For Uni ",
           	    (int)pageWidth/2-35, (int)(pageHeight
         	    		+fontHeight-fontDesent));
        	
         	g2.translate(0f,headerHeightOnPage);
         	g2.translate(0f,-pageIndex*pageHeightForTable);
    
         	//If this piece of the table is smaller than the size available,
         	//clip to the appropriate bounds.
         	if (pageIndex + 1 == totalNumPages) {
         		int lastRowPrinted = numRowsOnAPage * pageIndex;
                int numRowsLeft = tblProgram.getRowCount() - lastRowPrinted;
                g2.setClip(0, (int)(pageHeightForTable * pageIndex),
                    (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(oneRowHeight * numRowsLeft));
         	}
         	//else clip to the entire area available.
         	else{    
                 g2.setClip(0, (int)(pageHeightForTable*pageIndex), (int) Math.ceil(tableWidthOnPage),
                 (int) Math.ceil(pageHeightForTable));        
         	}
    
         	g2.scale(scale,scale);
         	tblProgram.paint(g2);
         	g2.scale(1/scale,1/scale);
         	g2.translate(0f,pageIndex*pageHeightForTable);
         	g2.translate(0f, -headerHeightOnPage);
         	g2.setClip(0, 0,(int) Math.ceil(tableWidthOnPage), (int)Math.ceil(headerHeightOnPage));
         	g2.scale(scale,scale);
         	tblProgram.getTableHeader().paint(g2);
         	//paint header at top
         	
         	return Printable.PAGE_EXISTS;
       }
    Last edited by payung81; 05-17-2006 at 03:52 AM.

Similar Threads

  1. Replies: 2
    Last Post: 11-15-2007, 06:10 PM
  2. Include Calendar in my web Page
    By Sinni in forum Web
    Replies: 0
    Last Post: 12-06-2005, 08:56 AM
  3. Bring Your User Interface to the Printed Page with the XtraPrinting Library
    By Developer Express in forum dotnet.announcements
    Replies: 0
    Last Post: 03-21-2002, 07:03 PM
  4. Replies: 1
    Last Post: 01-25-2002, 12:47 AM
  5. Replies: 0
    Last Post: 10-18-2001, 07:36 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links