1

Possible Duplicate:
Using Swing with a framebuffer

I've been searching all day for solutions in various directions to my problem. I'm working on Java running on the Kobo Touch, and I'd like to use Swing to render the components. The problem is, if I simply call paint() or its friends, the component in question will only render itself. If I use a JFrame to pack() the components, then it works on the desktop, but when I move it to the Kobo it fails because the Java environment is headless there.

In a headless environment, what is the simplest way to render a JPanel with children into a BufferedImage or Graphics2D?

Community
  • 1
  • 1
Ryan Kennedy
  • 3,275
  • 4
  • 30
  • 47

1 Answers1

2

Check this out:

Can I create a BufferedImage from a JPanel without rendering in a JFrame?

BTW, why is it headless? Kobo Touch does have a display...

EDIT: As the original poster found out,

getLayout().layoutContainer(this); 

is one way to force the layout manager to lay out the children if no frame is used, and therefore frame.pack() does not trigger the layout managers.

Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • 1
    It has a display, but it's not recognized as a GraphicsDevice... I have to write to its framebuffer at /dev/fb0 and then use their `pickel` command to refresh. – Ryan Kennedy Dec 16 '12 at 00:38
  • Also, I've attempted this solution... curiously, it paints the JPanel but none of its children. I've verified this by changing the JPanel's background color. I've also tried paintAll() and paintComponents(), to no avail. – Ryan Kennedy Dec 16 '12 at 00:53
  • If you do know of a way to get Java to recognize an arbitrary class as a GraphicsDevice though, let me know! – Ryan Kennedy Dec 16 '12 at 00:59
  • Are you sure you didn't forget the "child.setSize(child.getPreferredSize())" part? (if you don't call pack of a frame, the layout managers are not activated, and the children might have the size of 0x0) – lbalazscs Dec 16 '12 at 16:16
  • When I call `button.setSize(button.getPreferredSize())` on each child element, it resizes the elements to their smallest comfortable size and paints them in the upper-left corner. When I call `panel.setSize(panel.getPreferredSize())`, I get the panel's background color but no child elements. – Ryan Kennedy Dec 17 '12 at 16:17
  • This is good news, it means that you have no esoteric Kobo-Touch problem anymore (you would see the same on the desktop), the only problem is that your layout managers are not called. Experiment with panel.validate, child.setLocation, and ask a new question if it still does not work. It is also a nice thing to accept the answer, if it solved your original problem :) – lbalazscs Dec 17 '12 at 18:17