Swing painting pipeline – the conclusion
Over the past two weeks i have presented a few solutions to paint validation overlays in Swing. The sheer variety of the solution spectrum serves to highlight the extensibility of the Swing painting pipeline.
![]()
![]()
As can be seen from even such a simple example as validation overlays, there are many ways to achieve the desired functionality in Swing, each one having its advantages and disadvantages. This variety is, perhaps, Swing’s biggest blessing and biggest curse. On one hand, if you know how Swing works, you can achieve almost everything. On the other hand, if you’re a novice developer, it’s just too much to handle.
In addition, you need to be fully aware of the limitations of each technique to choose the one that best suites your needs. There is no single silver bullet that works best for all requirements, and in most cases the best solution would be a combination of two or even more working together. Hopefully this series has helped you put a few more tools in your developer arsenal. So the next time you need to provide some custom painting functionality, look at the Swing painting pipeline, weigh all the options and go with the one that would work best for your specific requirements.
To sum up, here are the links to all the entries:
- Repaint manager
- Overriding paint() manually
- Using AOP to override paint()
- Custom border
- Layered pane
- Glass pane
- JXLayer
- Extending look and feel
- Multiplex look and feel
Related posts:
- Swing painting pipeline – the introduction In my presentation at OSCON 2007 (PDF slides available) i talked about the Swing painting...
- Animation blueprints for Swing – the conclusion Concluding the series on adding animations to enable rich interactivity expected from modern Swing applications,...
- From Photoshop to code – step 3, implementing the decoration painting In this entry i’m going to talk about the third step in the process of...
- From Photoshop to code – conclusion This entry is going to summarize the process of taking the UI definitions from your...
August 7th, 2007 at 9:19 pm
Kirill, I have one question that just came to mind while working on a current problem, how are heavy weight components handled with a Layered Pane? Are the heavy weight components drawn last or is there one or more layers in between them and the glass pane? This could cure a current problem with my current project that uses JOGL. On linux the JOGL swing component, GLJPanel, is crashing the jvm, but the heavy weight counterpart, GLCanvas, does not and I need to draw Java2D items on top of the JOGL viewport. Will a layered pane solve this problem?
August 7th, 2007 at 9:54 pm
I don’t have any experience with heavyweight components, so i think that it would be better to ask this question at this forum.
August 8th, 2007 at 3:56 am
No lightweight solution presented by Kirill in the previous posts would work (Glass pane wouldn’t solve heavyweight either). There is work on this at Sun to “solve” heavyweights in the Java platform but currently the only “solution” is something similar to the way tooltips and popups work with heavyweights: use a Window. This is obviously a remarkably complex solution for something as trivial as that and poses many issues of its own…
Kirill, would you recommend I switch my existing GlassPane validations to use a layered pane?
August 8th, 2007 at 4:21 am
Krill,
Ur Swing painting pipleine articles are so good and informative ,they should be added into the standard java swing tutorial !
August 8th, 2007 at 8:17 am
Thanks Kirill, I will do that and I look forward to the next subject at hand.
August 8th, 2007 at 8:39 am
Follow up for those who may have similar problems. You can use heavy weight components with a layered pane. For more info check out this article http://java.sun.com/developer/JDCTechTips/2005/tt0118.html#1
August 8th, 2007 at 8:43 am
Pavan,
They are welcome to take these articles, as long as they’re not changed to use NetBeans and GroupLayout.
August 8th, 2007 at 8:49 am
Shai,
Although layered pane has its shortcomings, it’s IMO a better solution than glass pane. Whatever tricks you have for glass pane (to handle scroll panes, split panes, card layout etc) should work the same in layered pane. I’d recommend explicitly documenting what layer you’re using (or even provide an API so that the application can set the layer position). In addition, there is another scenario that i forgot to mention (which is relevant for all global techniques, including repaint manager and glass panes as well) – JXPanel.setAlpha allows setting the alpha channel for all its children.
The code needs to make sure that the validation icons are painted with the alpha channel of the relevant component. You can use SwingUtilities.getAncestorOfClass to see if a specific component lies inside a JXPanel (with possible usage of Class.forName so as not to have an explicit dependency on the SwingX library). Of course, this would handle only this specific component and not all “esoteric” usages of custom paint() implementations in the application code.
The “component-level” techniques don’t have this limitation as long as they respect the composite set on the Graphics object that they operate upon.
August 8th, 2007 at 8:50 am
realbadapple – thanks for posting the link on heavyweight panels.
August 8th, 2007 at 10:00 am
Thanks Kirill.
realbadapple, thats a nice link but notice that they specifically mentioned ZOrder and not painting… From my experience minor things break in painting/lightweight code when mixing heavyweight and lightweight on “some” platforms. This might have been fixed since I just don’t do it anymore ;-)
Generally things like component focus and regional native paints would just draw over any lightweight component regardless.
Notice that if this worked smoothly we would not need the heavyweight popups/tooltips… When working with heavyweight components they tend to “slide under” this can be patched but then you run into the rendering bugs.
August 11th, 2007 at 11:46 am
FYI, I just finished porting my glass pane code to use layered pane. The transition would have been relatively smooth but I just missed the call to revalidate (I need to install the layered pane myself on the fly)… That took me ages to realize since its not in the examples.
Thanks again Kirill, this does seem more elegant.
August 13th, 2007 at 5:01 pm
Kirill, I have followed javaDesktop for over a year now and would like you to know that the articles you write are excellent and always a compelling read. I get the feeling it might be a thankless job or at least not as many thanks as you deserve.
Keep up the good work, you do Swing proud.
August 13th, 2007 at 5:05 pm
Thanks, Greg.
August 27th, 2007 at 2:00 pm
[...] was reading Kirill Grouchnikov blog while getting a customer request to implement such a feature. Kirill wrote a series of blogs on [...]