Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java Swing Printing--Jlabels With Missing Characters

setpreferredsize printing

  • Please log in to reply
30 replies to this topic

#13 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 19 April 2012 - 06:50 AM

I thought you might have been right about this, but I commented out all the lines in the ActionListener which alter the PageFormat and Paper, and let it keep the default format and paper size, yet the issue remained.

Did your changes above work for you?


Yes, they worked for me.
  • 0

#14 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 19 April 2012 - 07:00 AM

Tried it with your changes. Also tried it with a GroupLayout instead of a BoxLayout. Both times, still no go.

I'm beginning to think this is an issue with the JDK.

Any other crazy ideas I could try? I'm willing to try anything at this point.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#15 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 19 April 2012 - 07:04 AM

This is exactly what I have (Just in case I left out a few changes)
http://pastebin.com/H8bn4i8B

Dunno why all the imports are listed twice. ?_? lol
  • 0

#16 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 19 April 2012 - 07:10 AM

Mine looks pretty much like yours at this point.

I did manage to figure something out, though. I used the GroupLayout's linkSize() method to link the horizontal size of all the JLabels, and as a result, the 'n' on the end of 'John' printed perfectly. However, the space between DOB and SSN was still negative width. This leads me to believe it's a font metrics issue. Perhaps between rendering in screen space and rendering on device space, something changes. I'd like to know what exactly is causing it though.

As a matter of interest, perhaps you can answer this for me (since I have a limited understanding of how Java's printing API works.) When I create the JPanel to print, but I do not add it to the JFrame on the screen, when I go to print it, nothing prints. It's just blank. However, when I do add it to the screen, and the components are realized as Swing constructs the JFrame, it does print.

If I can avoid having to display it in this way in order to get it to print, I'd like to do so. Is there a way to have Swing realize the components, but not in screen space, but rather in device space only? Perhaps then the font metrics issue wouldn't be a problem.

I'm just guessing here. Let me know if I'm way off base.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#17 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 19 April 2012 - 07:38 AM

I don't have hardly any experience with the printing api.

If you don't want the frame to be shown in the ui, and if you're just printing text, maybe you could use a BufferedImage object. Just get the Graphics object and draw your text to the BuffereredImage. (drawString(...))
Then use Container's print(Graphics g) method.
  • 0

#18 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 19 April 2012 - 07:46 AM

Would I still be able to use the benefits of a Layout Manager with that method? I'm trying to avoid having to absolutely position every element on the page.

EDIT: Found this in some old bug reports from Sun: (Perhaps relevant?)

It seems that JTextArea doesn't print to a printer properly. When the JTextArea
is painted on the screen, everything looks fine. When looking at the output
from a printer or printed file, the right side of the text area is clipped.
...
This is a bug in Swing since Swing caches the font metrics from a screen
context and then assumes they are valid in all contexts. This is incorrect
and needs to be fixed in the code that makes this assumption. eg in the
Swing implementation.

The basic technical issue is that screen fonts at small point sizes are tweaked
by the font vendors to look good at those sizes with the limited number
of pixels available at screen resolution.
At high reslutions such as obtained on printers this isn't scaling exactly
with the graphics transform.
This information is coming from the font in both cases.
There has to be either a compensation for this or an accomodation of it.
The usually accepted method is to measure text for the context in which
its going to be displayed. In this case swing is measuring at screen resolution
but displaying at printer resolution.

xxxxx@xxxxx 2002-02-16
============================


The fix to Swing will involve the following:
. Move all painting calls in Swing to SwingUtilities2.
. In painting check if we're printing and if printing use TextLayout
to layout the text just like it appeared on screen.
. All calls to get the FontMetrics should get the FontMetrics
from the Component. You shouldn't use the FontMetrics from the
Graphics as that isn't what we layed the text out.

Additionally this fix will consolidate some redundant painting code.
xxxxx@xxxxx 2003-11-24


Source: http://bugs.sun.com/...?bug_id=4352983

I know it's old, and it's not exactly the same scenario as what the bug report cited, but could any of this be relevant here?
(When my JLabel is allowed to remain at the preferred size calculated when drawing to the screen, it gets cut off in printing. However, if I expand the horizontal size manually, it prints fine.)
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#19 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 19 April 2012 - 08:51 AM

I'm at home, JDK 6. Same problem.

Changing font size to 7 or 9 fixes it. 8 is just evil :P
  • 0

#20 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 19 April 2012 - 09:31 AM

I'm at home, JDK 6. Same problem.

Changing font size to 7 or 9 fixes it. 8 is just evil :P


Lol. Just my luck.
I'm interested in learning more about how Swing actually realizes its components underneath the hood, so that I can hopefully print stuff without having to realize it in the screen graphics context first, and also so I can figure out how to have Swing position all its components using the printer's graphics context instead. Any tips?
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#21 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 19 April 2012 - 09:31 PM

As a matter of interest, perhaps you can answer this for me (since I have a limited understanding of how Java's printing API works.) When I create the JPanel to print, but I do not add it to the JFrame on the screen, when I go to print it, nothing prints. It's just blank. However, when I do add it to the screen, and the components are realized as Swing constructs the JFrame, it does print.

Everything (including the JPanel itself) will have its default size (0 I guess) if you don't add it to something. Because you add it to the frame, the layout manager kicks in and the layout manager will call setBounds(...) on the components to fit appropriately for the layout.
  • 0

#22 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 20 April 2012 - 05:15 AM

Everything (including the JPanel itself) will have its default size (0 I guess) if you don't add it to something. Because you add it to the frame, the layout manager kicks in and the layout manager will call setBounds(...) on the components to fit appropriately for the layout.


Thanks. Is there a way to force a layout manager to perform its layout other than rendering it to the screen?

EDIT: Just checked the documentation. I suppose I'm asking about LayoutManager.layoutContainer()...
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#23 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 20 April 2012 - 05:23 AM

I assume this does it:http://docs.oracle.c....awt.Container)
But the container you pass in must return something properly for getInsets(); so make sure to call setInsets first.
  • 0

#24 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 20 April 2012 - 05:38 AM

I see. In that documentation, it says,

Most applications do not call this method directly. This method is called when a container calls its doLayout method.

So I looked up Container.doLayout() and saw this:

Most programs should not call this method directly, but should invoke the validate method instead.

So would setting the JPanel's insets first and then calling validate() accomplish the same thing?
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Also tagged with one or more of these keywords: setpreferredsize, printing

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download