Now I added some code, may be could help, don't know about the quality,
and I didn't compile it ;-) Now I would go in this direction if I would
have ur problem, but I don't know if a desigh pattern exists for this
sort of problem. By sure I wouldn't integrate the ActionListener into
the JPanel, by sure not
class XX extends JPanel implements ActionListener
Only alternative could be to make an innerclass of JPanel implement the
interface.
About the actionListener, u could use Action instead, is more powerfull!
Hope I could help a little
Patrick
p.s. I guess this is the wrong mailing list for this questions, check
out some others may be
[EMAIL PROTECTED]
could be more adequate.
Riyad Kalla wrote:
and Patrick added ;-)
/**
* This has to be an interface, because you cannot extend all component
classes?
*/
public interface ConstraintsComponent
{
/**
* Provide the component to be added.
*/
Component getComponent();
/**
* Provides the constraints for the passed LayoutManager,
* which should be used adding the provided component.
*/
Object getConstraintsForLayout(LayoutManager lm);
}
public interface TargetProvider
{
Container getTarget();
}
> /* Class Listener */
> public class iHearYou implements ActionListener
> {
private TargetProvider targetProvider;
public iHereYou(TargetProvider targetProvider)
{
super();
this.targetProvider = targetProvider;
}
protected ConstraintComponent createComponent()
{
return new ConstraintsComponent() {
public Component getComponent()
{
return new JLabel();
}
public Object getConstraintsForLayout(LayoutManager lm)
{
if (lm instanceof BorderLayout)
return BorderLayout.NORTH;
return null; // for unknown lm
}
};
}
> actionPerformed( ActionEvent e )
> {
if (targetProvider != null) {
Container target = targetProvider.getTarget();
if (target != null) {
ConstraintComponent newCmp = createComponent();
target.add(newCmp);
}
}
> // Do something complex, create a new graphical
> component as a result
> // that needs to be added to the UI.
> }
> }
>
> /* Class Panel */
> public class controlMe extends JPanel implements TargetProvider
> {
public controlMe()
{
super();
> JButton a = new JButton( "Hi!" );
//> iHearYou newEar = new iHearYou();
iHearYou newEar = new iHearYou(this);
> a.addActionListener( newEar );
> add( a );
}
public Container getTarget()
{
return getParent();
}
> }
>
> /* Class UI */
> {
> controlMe S&M = new controlMe();
> getContentPane().add( S&M );
> }
>
--
-----------------------------------------------------
Save software competition, use Linux and Java!
mailto:[EMAIL PROTECTED]
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]