This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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:
Joined: Jul 31, 2006
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.
Joined: Jul 31, 2006
Figured it out myself. I just needed to implement Pageable and set the correct PageFormat in the getPageFormat() abstract method. Works perfectly now.