Drawing Java Components without displaying them.

Recently I have had a need to draw a pdf page to a provided graphics object. Considering that we draw the page to a Panel this is no hard feat to achieve all we need is to pass the graphics object into the Panels paint method. The problem we have run into is that we need to do this without displaying the Panel itself.

For most of the pages content this is no problem and the output is correct. Form components on the other hand are not so simple. By default our form components are swing components added to the Panel so as to appear at the correct point on the page. Where we have custom appearances we can override the display of the components.

When passing in a provided graphics object everything appears to be rendered correctly but some forms are not displayed correctly, they appear as empty grey boxes on the page. I have spent some time looking into this and I have come to learn several important lessons.

  • The process of overriding the appearance of the components allows the form component to be drawn on the graphics object correctly.
  • Form components that use the standard appearance are not displayed correctly when they are not visible on the screen.
  • Swing components display appearance only seem to be created or made draw-able once a component is made displayable.
  • These components are only made displayable in certain circumstances.

So, what is the difference between displayable and visible. A visible component is currently visible on the screen. A displayable component means it has been added to a containment hierarchy that has been made displayable. This can be done by either calling pack() on the ancestor window or by making the ancestor visible.

So in order to allow the pdf page to be drawn to a graphics object and have all the form components appear correctly without displaying the pages to screen we need to add the form components to a dummy frame. By adding the form components to a JFrame and then calling pack() from the JFrame we can mark the for components and any children components as displayable.

Problem solved.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

Related Posts:

The following two tabs change content below.
Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He often has no idea what to write in his blog posts but tries his hardest to make them interesting and entertaining, he also makes no excuses for his odd sense of humor.
KieranF

About Kieran France

Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He often has no idea what to write in his blog posts but tries his hardest to make them interesting and entertaining, he also makes no excuses for his odd sense of humor.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>