开发者

Painting a JComponent without adding it to a container

开发者 https://www.devze.com 2023-03-13 16:09 出处:网络
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

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 JComp开发者_如何学编程onent 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.


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.

Painting a JComponent without adding it to a container

The important line of the source is..

textLabel.setSize(textLabel.getPreferredSize());


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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号