IAmYourFaja
IAmYourFaja

Reputation: 56914

New Swing JLayer in Java 7

I am just catching up with some of the new features in J7 and am perplexed by the addition of these JLayers in Swing. Since they're so new I am having tremendous difficulty finding good literature on them and the best practices of using them.

Could anybody point me in the right direction or provide an example of what these components do and what purposes they (generally) serve?

Thanks for any input!

Upvotes: 2

Views: 3055

Answers (1)

Matt Wonlaw
Matt Wonlaw

Reputation: 12443

A JLayer can be used to dynamically enhance any of your existing components.

Say you have some custom button (say MyJButton) and you want to add a mouse over effect or maybe some shading. Traditionally you would create a new class that inherits from MyJButton that would implement your new features.

One problem with this is approach is that the new effects are only applicable to MyJButton (since your new class extends MyJButton). Another problem with this approach is that you can't change the enhancements / effects at run time, since inheritance relations are fixed at compile time.

Using a JLayer, you can put a LayerUI together with any existing Component (not just a JButton or MyJButton) in order to add your custom effects to a component. If you have a LayerUI that does shading, you can put it together with a JButton, JTextField, JPanel or any other component to perform that shading. You can also change the composition of LayerUIs and Components at run time.

The JLayer is just a special case of the decorator pattern.

http://www.intermediatejava.com/2011/06/jlayer-for-swing/

Upvotes: 3

Related Questions