Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing a JPanel

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.

Thanks!
 
Pablo Vadear
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Figured it out myself. I just needed to implement Pageable and set the correct PageFormat in the getPageFormat() abstract method. Works perfectly now.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
...
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic