File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Printing a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply locked New topic

Printing a JPanel

Pablo Vadear

Joined: Jul 31, 2006
Posts: 3

First, I hope this is the right area. I did search and I found most of the questions regarding JPanel printing are in this section. I also found that none of the ones that seemed to be similar to my problem had any responses. I hope I have better luck.

My deliverable is a program that prints ID cards on a special card printer. It should also show a scaled up preview of the card for proofing before sending the job to the printer. I've decided that to make the process as simple as possible I would have two JPanels, one for the preview and one for printing. That's not my issue.

My issue is this: I was having a lot of problems with scaling the print job and figuring out how to adjust the paper size to the card's size, so I created a simpler project that is very basic. It shows the JPanel (actual size) and has a print button to print it. The problem is that when I do print it, the first page only has a narrow vertical rectangle on it in the color of the JPanel background. Subsequent pages print correctly.

Also, it prints way to many pages. It should only print one, but since I'm a little confused on how to translate the pixel size of the JPanel to the 1/72" scale used to define the paper it doesn't seem to work right.

This is my first Java program that requires printing so I'm still pretty new at it. Please keep that in mind when you look at the source for my test project below and try not to laugh.

Here's the source minus package declaration and imports:

Pablo Vadear

Joined: Jul 31, 2006
Posts: 3
I've made a little progress on my own, but now I'm getting weird orientation results.

I moved the PageFormat set up to the calling class and I'm passing it to the print() method now. When I had it in the print() method before the first page would print the component in landscape (wider than tall) on portrait oriented paper but the second one would print the component in portrait (basically rotated 90 degrees counterclockwise from the "landscape" orientation of the first page), and so would print correctly, in other words then entire component appeared as desired on the second (and subsequent) pages.

Now that I moved the PageFormat to the calling class, when I select PageFormat.PORTRAIT it prints the component landscape starting in the upper left corner of the page it prints the upper left corner of the component across the page, cutting it off on the right side.

If I use PageFormat.LANDSCAPE it rotates the component 90 degrees conterclockwise on landscape oriented paper (upper left corner of component starting in lower left corner of the page).

I'm completely baffled and I've found no help via Google, these forums, Experts-Exchange, the Sun Java forums, books, magazines, 900-numbers, messages in bottles, or the Psychic Friends Network, so I would really appreciate whatever assistance anyone can provide.

Pablo Vadear

Joined: Jul 31, 2006
Posts: 3
Figured it out myself. I just needed to implement Pageable and set the correct PageFormat in the getPageFormat() abstract method. Works perfectly now.
Raghunandan Kavi

Joined: Nov 15, 2010
Posts: 1
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10733

Welcome to the Ranch.

Please do not post your questions in other people's threads. You can start your own thread by clicking on the at the top of this page.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
subject: Printing a JPanel
It's not a secret anymore!