pdinklag
pdinklag

Reputation: 1260

JScrollPane and printAll()

I am working on a JPanel extension that is only for rendering its contents to a BufferedImage, which I then use as a texture for rendering in OpenGL. For this, I create my special JPanel extension with a layout manager etc., like I would if I was building a normal Swing GUI.

In order to render it to an image, I make sure to call doLayout first and then I use the printAll(java.awt.Graphics) method to print it and the added components:

    Graphics2D g = image.createGraphics();
    printAll(g);

Swing itself (EDT, RepaintManager etc) is never involved, I just create the components (outside any window or frame) and directly use printAll.

This is working perfectly well for most "atomic" components (such as buttons, labels, lists), but I hit on a problem with the JScrollPane. Whatever I try, the JScrollPane will never paint its viewports (the main viewport and the scroll bars) to the given Graphics but only its border, and even manually calling printAll on the viewports yields no result.

I attached two screenshots which show a "before and after" of a simple GUI consisting of a JList and a JButton. On the first image, I use the raw JList, in the second image, I wrapped it into a JScrollPane using new JScrollPane(list).

Using a JList directly Using a JList wrapped in a JScrollPane

I failed to find any magic inside the JScrollPane code related to painting its viewports. Maybe something needs to be initialized that Swing would do, but I forgot. Is there any way I can get those viewports to show up?

Upvotes: 1

Views: 850

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168825

See Why does the JTable header not appear in the image? for some general tips about rendering components to images. Try calling combinations of validate(), addNotify() & doLayout() as mentioned in the code of that thread.

Upvotes: 1

mKorbel
mKorbel

Reputation: 109813

not sure from your question

1) if there is about JScrollPane or JViewport, becasue JViewport is only (from JScrollPane) about visible Rectangle on the screen,

2) every JComponent returns Graphics2D

3) please why do you call doLayout() / addNotify(), in the case that all events for Swing GUI are done on EDT, or are there some events based on Swing Timer

4) better would be edit your question with SSCCE that generated un_wanted output to the GUI

5) for JViewport you have to create and override your own RepaintManager

6) maybe stupid question, please ensure us that there isn't any animations of JComponent, meaning moving accross JScrollPane and freezed on EDT by Thread.sleep(int)

EDIT (could be most important)

7) for OpenGL you have to implements Components based on Java AWT

Upvotes: 1

Related Questions