Please help me, I'm a newbie too!

Hi all,

I think that my question is stupid, but I don't know how to solve it.

I have a class A like this:

import javax.swing.JFrame;

publicclass Aextends JFrame{

private BButton bb =null;

public A(){

super("Passing class...");

bb =new BButton();

getContentPane().add(bb);

pack();

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicstaticvoid main(String[] args){

new A();

}

}

that use the following class:

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

publicclass BButtonextends JButton{

public BButton(){

super("Please Click me!");

addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent ae){

System.out.println("Clicked, but from who?");

}

});

}

}

My trouble is: how can BButton knows which class istanciated it? In other words, in the BButton point of view, how can it know that was class A to istanciated it?

Thanks in advance.

Pier@

[2423 byte] By [pier@a] at [2007-10-3 4:59:17]
# 1
BButton shouldn't care "who" instantiated it, if you want to achieve better de-coupling. If it really does need to know though, you could pass the A object to it, either in BButton's constructor, or another method call. You'd have to change BButton's code of course to do this.
warnerjaa at 2007-7-14 23:04:41 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks a lot, I thought that there was a "better" metod apart from passing the reference object directly, but perhaps I didn't know it because there isn't one...:))
pier@a at 2007-7-14 23:04:41 > top of Java-index,Java Essentials,New To Java...
# 3
Did someone say pass-by-reference?
aniseeda at 2007-7-14 23:04:41 > top of Java-index,Java Essentials,New To Java...
# 4
> Did someone say pass-by-reference?No. And stop pretend like angle all of you.
cotton.ma at 2007-7-14 23:04:41 > top of Java-index,Java Essentials,New To Java...
# 5
> > Did someone say pass-by-reference?> > No. And stop pretend like angle all of you.I was angling for a rotten debate. :P
aniseeda at 2007-7-14 23:04:41 > top of Java-index,Java Essentials,New To Java...