Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Printing a jtable in Landscape

843804May 15 2005 — edited May 17 2005
Hi all,

Well, I'm currently trying to print my extended version of JTable, but I am not having too much luck. I'm trying to print on a 8.5" by 11" piece of paper in Landscape format. I was wanting about a 1/2 inch on the left and right sides of the table and a 1" margin on the top and bottom of the jtable from the end of the piece of paper.

I thought I had it almost as I wanted it except the JTable is not getting printed like it should. I think it might be a scaling problem. The first column and sometimes even more of the beginning of the JTable fails to be printed. The code for initializing a print session is below.

Does the width and height in landscape go to being reversed (i.e. now width = height of paper; height = width?)
if( event.getSource() == printButton )
{

	PageFormat x = new PageFormat();
	x.setOrientation( PageFormat.LANDSCAPE );
	Paper f = new Paper();
				
	f.setImageableArea( 72, 72, 792, 864 ); 

	f.setSize(792, 864) ;
				
	System.out.println("Height: " + f.getHeight() );
	System.out.println("Width: " + f.getWidth() );
				
	System.out.println("x: " + f.getImageableX() );
	System.out.println("y: " + f.getImageableY() );
				
	x.setPaper( f );

	PrinterJob printJob = PrinterJob.getPrinterJob();
	table.setShowGrid( false );
	printJob.setPrintable( (exTable)table, x );

	if ( printJob.printDialog() )
	{
					
		try
		{
			printJob.print();
		}
		catch ( PrinterException pe )
		{
			pe.printStackTrace();
		}
	}

	table.setShowGrid( true );
	table.revalidate();

}
This is the code that over rides the print method in my version of JTable from the Printable interface. Most of the code is from a previous forum post, and it's hardly changed.
public int print( Graphics g, PageFormat pageFormat, int pageIndex )
{
		
	Graphics2D g2 = (Graphics2D)g;
         	g2.setColor(Color.black);    //set default foreground color to black

        	RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
        
        	Dimension d = this.getSize();    //get size of document
        	double panelWidth  = d.width;    //width in pixels
        	double panelHeight = d.height;   //height in pixels
        	double pageHeight = pageFormat.getImageableHeight();   //height of printer page
        	double pageWidth  = pageFormat.getImageableWidth();    //width of printer page
        	double scale = pageWidth/panelWidth;
        	int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);

        	// Make sure not print empty pages
        	if(pageIndex >= totalNumPages)
        	{
        		return Printable.NO_SUCH_PAGE;
        	}

        	RepaintManager currentManager = RepaintManager.currentManager(this);
   		currentManager.setDoubleBufferingEnabled(false);

        	// Shift Graphic to line up with beginning of print-imageable region
        	g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        	// Shift Graphic to line up with beginning of next page to print
        	g2.translate(0f, -pageIndex*pageHeight);
        	// Scale the page so the width fits...
        	g2.scale(scale, scale);
        	this.paint(g2);   //repaint the page for printing
        	
	return Printable.PAGE_EXISTS;
}
Any tips, recommendations would be much appreciated.

Sincerely,

GCS584
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 14 2005
Added on May 15 2005
8 comments
578 views