Search |
||
Why I don't use Swing hacks (continuation)Posted by alexfromsun on October 9, 2007 at 4:50 AM PDT
In one of my previous blogs I answered to Kirill's comment that some of techniques he described in his Swing painting pipeline overview are "not good enough for me",
here I want to explain my point by brifely commenting some of the techinques he covered. Implementing validation overlaysFirst of all I want to mention my requirements to the validation overlays implementation:
RepaintManagerWhen a component requires a custom RepaintManager to be installed it sounds for me as strange as if I require to customize your operating system for my application.RepaintManager is a global resouce and changing it to support a particular component is not just awkward but it may cause problems if another component wants to install its own RepaintManager at the same time. Since Swing repaints components asynchronously it is impossible to fix this potential conflict by saving and restoring the old RM after repaint() is called. Note: SwingX uses custom RM for their needs but it's a special case. SwingX is a project which contains extentions for the Swing GUI toolkit which may become a part of the JDK. If we decide to include e.g. translucency to Swing, the default RM will be the best choice to add support for this feature and no doubt, in this case their experience will be very useful. Since a custom RepaintManager works well if you are sure that no one else will change it, I take RepaintManagerX as the only recommended alternative implementation GlassPaneFor me it is quite obvious that GlassPane is not the best solution for validation overlays.I know that with GlassPane it is impossible to write a robust code which will quickly and correctly:
JLayeredPaneIt is the most interesting hack and it deserves special attention because of two remarkable projects.Long time ago I came across the Decorating/Overpainting Swing Components blog with an impressive demo by Timothy Wall. He transparently adds all kinds of custom components to the frame's default JLayeredPane to decorate existing components. The demo is really cool, but...
- you can make it work for a demo, but it will never be completely ready for production use The IconFeedbackPanel by Karsten Lentzsch is another tricky solution. I wrote a simple test with a JTextField inside IconFeedbackPanel with a validation icon, then I wrapped the JTextField with JScrollPane and the icon unexpectedly was gone. My first though was that IconFeedbackPanel doesn't support inner scrollPanes at all but then I read this note from its constructor javaDoc: Note: Typically you should wrap component trees with getWrappedComponentTree(ValidationResultModel, JComponent)}, not this constructor. When I tried the magic getWrappedComponentTree method the validation icon appeared in the inner JScrollPane and were repainted very quickly during scrolling. After a few minitues I found the reason - this method traverses the component's hierarchy and wraps JScrollPane's views with additional IconFeedbackPanels! This is an important detail, I'll return to it later The Karsten's code is clear and well-written as usual, only a few things about IconFeedbackPanel caught my attention
to make it work you have to wrap every JTextField with a kind of an IconFeedbackPanel Nevertheless, among all mentioned techniques, IconFeedbackPanel is probably the best one JXLayerIn his comments Kirill mentioned an interesting term - "component-level techniques", comparing JXLayer with GlassPanes and LayeredPanes.Actually if you need to decorate an existing component you have only three recommended choices:
That what I meant when I said that solutions like GlassPanes or LayeredPanes are not good enough for me. I want to be sure that my components will work well with all existing layouts, like StackLayout Try to make a component which is decorated with JLayeredPane, correctly overlap a non-opaque sibling component In the previous blog I showed two examples of validation decorations: when it is completely within the bounds of wrapped component, and JXLayer has the same size as its inner component; the second variant is when decorations are partially outside (the little red icon on the corner). I personally like the first one, because it doesn't affect component's layout and doesn't require a bunch of listeners to be added to the TextField's document. If I needed to support that fancy outer icon for some TextFields I would use Karsten's trick - he automatically wraps JScrollPane's views only when I'd wrap all TextFields to set them identical insets ConclusionSometimes the "no-hacks rule" is not easy to follow and may require some extra work,but use it or not is always up to you Thanks alexp »
Related Topics >>
Swing Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|