0

I am using JPanels to simulate a print preview and just printing the content panel, however I have ran into a problem whereby if I try to print multiple panels that are essentially the same document, only the the one currently being displayed on screen will print.

Is there a way I can force the JPanel to repaint even if it is not currently on screen?

I have tried:

  • Disabling double buffering via:
    • JComponent.setDoubleBuffered()
    • RepaintManager.setDoubleBufferingEnabled()
  • Painting Twice
  • Painting through paint()
  • Painting through repaint()
  • Painting through print()
Lerp
  • 2,957
  • 3
  • 24
  • 43
  • Try `doLayout()`, for [example](http://stackoverflow.com/q/7369814/230513). – trashgod Oct 21 '13 at 11:47
  • @trashgod Tried it, no luck. – Lerp Oct 21 '13 at 11:49
  • [some musings](http://stackoverflow.com/a/7372045/203657) – kleopatra Oct 21 '13 at 11:54
  • Have you tried [SwingUtilities.paintComponent](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#paintComponent%28java.awt.Graphics,%20java.awt.Component,%20java.awt.Container,%20int,%20int,%20int,%20int%29)? – VGR Oct 21 '13 at 12:31
  • @VGR I was just messing about with that. I don't know whether I'm using it wrong but it stopped all pages from being painted. – Lerp Oct 21 '13 at 12:34
  • @kleopatra I have looked into your linked thread and I now understand the component has to be "displayable" before it will be painted. I did the methods described but got no luck even though the component **is** saying it's displayable, I'll add code to my main post shortly. – Lerp Oct 21 '13 at 13:46
  • Okay, now I am truly confused. I was putting together an SSCCE and that works perfectly and I cannot see the disparities between the SSCCE and the erroneous code. :S – Lerp Oct 21 '13 at 14:21
  • I have worked out the discrepency. The components not wanting to print are actually `java.awt.Container`s... – Lerp Oct 21 '13 at 14:29

1 Answers1

0

I couldn't get it to paint off screen and I felt like I was wasting my time battling the Swing API so I just cheated and made the panels display on screen as they are being printed; now I have a new "feature" that shows you the page being printed.

I'll leave the question open incase someone knows how to do this as I would prefer not to shove all the pages in the user's face.


Update

Turns out the problem was with components that had extended java.awt.Container, the Swing components must override certain AWT methods that deal with this sort of rendering.

Lerp
  • 2,957
  • 3
  • 24
  • 43