Shevek (shevek) wrote,
Shevek
shevek

If you've written code in the Swing widget toolkit, you'll be familiar with the dual MVC model it uses: The Model (data model) provides data to the JComponent, but the JComponent itself is a model for the UI. So, for example, a JTextField has a Document as its data model, and it also stores a Font, some Actions and various other bits and pieces, but it has no idea what to do with these. The actual UI is rendered by the UI plugins, commonly called the Look and Feel.

The application developer has no control and usually no knowledge of the Look and Feel, which leaves something of a hole in the Swing object model - there is (initially) no way to override or modify the rendering of a component - to annotate it, decorate it, etc.

But there is, and it's very simple. The call into the UI is from the paint() or paintComponent() method, with a Graphics as an argument. The JComponent can do whatever it likes in that call. When I write trivial JComponents, I render directly. But that still isn't enough to make it simple or easy.

Enter JXLayer.

This under-reported and under-used library is pure magic, and the demos need to be seen to be believed. It provides a very simple extension for modifying and hacking the rendering of a JComponent without knowing any details of the UI plugin. It transparently wraps the JComponent's paint method and provides an event model to trigger a repaint when anything in the rendering hack changes, as well as when the original component changes. Wrapping the paint() method allows, for example, substituting the Graphics for a DoubleBuffer, to which image effects or transforms can be applied, and in my most common use case, rendering custom backgrounds and icons around JTextFields to provide context-dependent assistance to the user in a more flexible (and portable) way than setBackground, setTooltipText and so forth can hope to offer.

And the last magic missing piece is the replace() method in GroupLayout, which allows one to design a UI in Matisse, NetBeans's UI designer, and wrap components in JXLayer later.

I would also like to offer tribute to the authors of the SwingX widgets, without whom my job would be infinitely harder and uglier. Many of the features of SwingX are making their way into JDK proper, but the full, extended toolkit, once integrated into Matisse, is a wonder.
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments