1

I've implemented a custom JPanel, whose paint method I've extended to do a lot of manual rendering in full screen mode. Now I would like to integrate another JComponent to this (in my case a JPanel that contains a JScrollpane with a JTextPane as its viewport) that should appear on top of my first panel, but because my custom rendering pipeline is complex, adding the JComponent to my panel and having it painted the traditional way through the AWT system is not an option (I tried and it's quirky at best, not functional at worst), so my question is: is it possible to manually order the JComponent to be painted at one point in my program by calling its regular paint method without tying it to a JContainer and if yes, how do I do this?

Thanks in advance for your answers.

Tamori
  • 97
  • 9
  • N.B. If you cannot get it working with the advice already offered, I suggest you post an [SSCCE](http://pscode.org/sscce.html) of your best effort. – Andrew Thompson Jun 15 '11 at 16:49

5 Answers5

3

See the LabelRenderTest.java source on this thread. The label is eventually drawn to screen, but it is painted to BufferedImage before ever being displayed.

The important line of the source is..

textLabel.setSize(textLabel.getPreferredSize());
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Also consider `RenderingHints.VALUE_ANTIALIAS_ON`. – trashgod Jun 15 '11 at 19:58
  • @trashgod: Isn't anti-aliasing automatically enabled for a `JLabel`? Or are you referring to the rendering of the `BufferedImage`? I would not have thought it was relevant at that stage. – Andrew Thompson Jun 15 '11 at 20:05
  • The hint should be set on the context returned by `createGraphics()`, before any painting. Conveniently, `createGraphics()` returns a `Graphics2D` object, and it should get a `dispose()` when done. – trashgod Jun 15 '11 at 20:33
3

You can take a look at CellRendererPane and see how for example BasicTableUI paints component images with it.

Walter Laan
  • 2,956
  • 17
  • 15
2

Yes, just call the normal paint method on the object and pass the Graphics you want it to paint on. However, this is just going to paint it and it sounds like you want it to possibly scroll which means you will need to add it to your custom JPanel. In that case just add the panel and you a layout manager that will place the component where you need it.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • Thanks for your reply but this doesn't work. I call the paint method of the JComponent inside the paint method of my JPanel (and passing it the Graphics object) but the JComponent isn't rendered. It does render when I add it to the panel, but as I explained I don't want that because the result is very suboptimal, to say the least. – Tamori Jun 15 '11 at 15:07
  • Are you painting it before of after you do other painting in the panel? – jzd Jun 15 '11 at 15:10
  • In my program it should paint somewhere in the middle of my rendering pipeline, but for testing purposes I've tried adding it right at the end of the paint method but it doesn't work. I've also made sure I reset all the transforms, colour, strokes etc of the Graphics2D object before passing it to the paint method of the JComponent. – Tamori Jun 15 '11 at 15:23
2

You should set size for the component. Then to position it use your Graphics' translate(x,y) to position the component in desired Point.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Size of the component is set positioned on initialisation. But the component doesn't show when I invoke its paint method. – Tamori Jun 15 '11 at 15:09
0

if there is any container higher level in the hierarchy you can use

validate(); repaint(); 

pair to do that.

if not you can change it's size or bounds ( like +1 , -1 ) at the end to make it repaint itself.

Ugur Kumru
  • 986
  • 6
  • 9