Impementing the Decorator Pattern for a large super class

Say I want to make some JComponents that change colour when the mouse cursor is within their bounds. I'd like to do this for several components, so I'd probably use the decorator pattern.

However, overriding all the JComponent methods in the abstract decorator class to call the associated methods in the decorator's concrete JComponent field seems a very monotonous and lengthy task .

JComponent is the superclass

the swing components are what I call"concrete components"

publicclass Decoratorextends JComponent{

JComponent concreteComp;

// JComponent Methods

publicvoid myJComponentMethod(args...){

concreteComp.myJComponentMethod(args.....);

}

// line upon line of boring code...

// end of JComponent methods

}

publicclass ColorChangerextends Decorator.....

Is there a quick way to assign the methods of concreteComp to the methods of Decorator? I thought about using java.lang.reflect to get method pointers for the concreteComp field but I wouldn't know how to assign these to my decorator class anyhow.

Has anyone got any ideas? Thanks,

[1640 byte] By [eclipsicala] at [2007-10-2 16:33:56]
# 1
Why do you want to use a decorator for this? What's wrong with extending mouse adapter and calling the methods on the components to change the colour?Pete
pm_kirkhama at 2007-7-13 17:38:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Hey, thanks for replying

Extending mouse adapter is definately the way i'd go if I just wanted to adapt one type of swing component. Here however I'd like to do this for several components in the api. Buttons and Panels for example.

I could just extend mouse listener for those classes but I was wondering if it could be done more generally. Afterall this is the classic example of the decorator pattern, but it doesnt seem to be practical to implement!

eclipsicala at 2007-7-13 17:38:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
If you want to basically override or augment the components' paint methods, take a look at this article and demo: http://rabbit-hole.blogspot.com/2006/04/decorating-swing-components.html http://abbot.sf.net/demo/DecoratorDemo.jnlp
twalljavaa at 2007-7-13 17:38:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

There are practical limitations to the applicability of the Decorator/Facade patterns. Swing's extensive API makes it a bit difficult, although you could get mileage out of a wrapper base class that delegates *all* its methods to the target. Then your derived class need only override those methods of interest.

One thing to note though, is that Swing components have an awareness of whether they are actually within a component hierarchy or not, so you may get unexpected behavior delegating to a component which has not actually been added to your hierarchy.

twalljavaa at 2007-7-13 17:38:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

I am nto sure it is a decorator pattern...

Decorator pattern is something , like during runtime, you will be creating the objects from another object and thus saving the resource and time by creating the object from instance.

Extending the functionality is not the Decorator pattern.

redwina at 2007-7-13 17:38:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

>Extending mouse adapter is definately the way i'd go if I just wanted to adapt

>one type of swing component. Here however I'd like to do this for several

>components in the api. Buttons and Panels for example.

You're actually adapting one type of component, namely java.awt.Component. This is what supports mouse listeners and changing color.

Btw, a decorator that changes a couple of properties doesn't have to modify (i.e. decorate) the behavior of the target object, but can just do that - change the properties of interest. Only if the properties were not settable (no setFooProperty method) it would be necessary to use the Decorator pattern.

ounosa at 2007-7-13 17:38:44 > top of Java-index,Other Topics,Patterns & OO Design...