• Post Reply 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

Print a JComponent (JPanel) using javax.print instead of java.awt.print

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
Could please someone show me some "simple" code (or where to find it) for printing a JComponent (in particular a JPanel with all it's content) using javax.print?
I already tried to use java.awt.print but run into a problem in uneven spacing in text objects.
Thanks in advance
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to be more specific in what you mean by "print a JComponent" What is it you are trying to do?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I interpret the requirement as reproducing the component on a piece of paper.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having looked at the API documentation for javax.print, that package appears to be mostly dedicated to getting something which is already in printable form to a "printer". It leaves it up to other code to produce a Doc object which can be printed. Therefore Swing has some code whose purpose is to do that, and that's the code you don't like.

I remember when I used the java.awt.print classes to print data, I noticed that it laid out the text really badly: letters were the wrong width and overlapped the adjacent letter, or there were excessively large spaces between letters. That was about 10 years ago and I couldn't find any way to deal with so I just left it alone. I never printed anything much so it didn't really annoy me. But now when I try my old code, the text comes out perfectly. I don't know what changed in the meantime (except for upgrading from Java 4 or 5 to Java 8).
 
Luca Vanon
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for the replies.
First of all, with print I mean send the content to a printer (paper or PDF doesn't matter, must work for both because it will be the end-user of my software that will decide).
I was searching for an alternative for java.awt.print exactly for the problems that Paul was talking about, the problems with uneven spaces between words.
Fortunately at the end I found the solution in using HTML content and the non-breaking space tag  .
This someway force the scpace between words and avoid the overlapped words.
I found also useful the non-breaking space tag at the end of a line to avoid strange clipping behaviour when using special characters like "/".
So, if someone has the same issue the solution is:
 
 
Luca Vanon
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, since it is HTML code, I noticed that the solution is missing...
The solution is: & n b s p;
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic