add listener to listen other component change

Hi,

I have a component class C1 that mainly contains a JTextField and I hope it can change text value when a button in another component class C2 is pressed.

If there's no access restriction, I know I can addActionListener to that button like:

myButton.addActionListener(new ActionListener(){

public void ActionPerformed(e){

String textStr =button.getText() ;

myTextField.setText(textStr);

}

});

But for security reason, I don't want C1 has direct access to C2's textField. In stead, I hope to add event listener in C2 for the textField, so that the textField can get noticed when button press event occurs. It will check the button's actionCommand and decide whether to get the button's text or not.

If I use:

myTextField.addActionListener() it's actually listen to event that occur on the textField component, not any event occur on other components.

AbstractButton class has "fireActionPerformed()" method but it's protected and I don't know how to use.

Is anyone know how to achieve this?

many thanks

[1112 byte] By [angelflareea] at [2007-11-27 11:07:45]
# 1

> Is anyone know how to achieve this?

yes, but it's probably already been answered on comp.lang.java.gui and other

places you have posted, so I won't bother spending any more time on your

posts other than this comment.

Michael_Dunna at 2007-7-29 13:24:07 > top of Java-index,Desktop,Core GUI APIs...
# 2

Well, I hope so.

But different people or expert tend to focus on different forum.

If you know the answer and you visit multiple forums, at least please answer once, if you don't hope to spend time to see repost .

angelflareea at 2007-7-29 13:24:07 > top of Java-index,Desktop,Core GUI APIs...
# 3

OK, a bit more time, educational purposes only.

> But different people or expert tend to focus on different forum.

then, quite simply, pick the single forum you consider most likely to provide

an answer to your problem.

If unsuccessful, post in another forum, but it is essential you start the new post

with something like "I've already posted this in [forum], but have not had a solution",

and supply a link so that the 'new' readers of your problem can see what has

already been suggested/discarded and do not waste their time repeating the

same stuff.

>... if you don't hope to spend time to see repost .

repost as may times as you want, I won't be providing any answers to your posts, others

will, but, in time, they too will see they have wasted their time on duplicate posts

and will add you to their ignore list.

Michael_Dunna at 2007-7-29 13:24:07 > top of Java-index,Desktop,Core GUI APIs...
# 4

one way of doing this is to define an listener interface, called C1Listener, that can have a method called: valueChanged(C1). Then make C2 implement the listener interface.

also, make a reference to C1Listener in C1 and add some method to notify the listener, e.g. fireValueChanged()

class C1{

...

C1Listener lsn = new ImplementedC1Listener();

fireValueChanged(){

lsn.valueChanged(this);

}

}

class C2 implements C1Listener{ //ImplementedC1Listener

...

c2value;

valueChanged(C1 c1obj){

c2value= c1obj.getc1value();

}

}

angelflareea at 2007-7-29 13:24:07 > top of Java-index,Desktop,Core GUI APIs...
# 5

Yes, I always post on one forum first but if I didn't get reply for a half day. I will try other forum to increase the chance of reply and early reply. It's not against any forum's rule, isn't it?

Why should I tell others that I've post on other forum if a lot of people won't visit multiple forums and not many really interest in my post. I will do if I get meaningful reply of course. Otherwise, it's also a waste of my time and their time to see dup post with no answer.

Even people see repost of the same question, I think most won't reply multiple times so it won't annoy most people.

This is my style of posting. If you don't like, you can ignore.

angelflareea at 2007-7-29 13:24:07 > top of Java-index,Desktop,Core GUI APIs...