macgre
asked on
Universal Java ActionListener
How do I create a single listener that will be informed of every button press (for starters).
I looked at AWTEventListener with the mask ACTION_EVENT_MASK but I didn't receive any events! I am attaching my listener code.
I am attempting to build my own debugger/tester as a learning experience and need to monitor all occurances of various events. I am just starting and the first event I am having trouble with is action events. I have been able to determine whenever a button gets added using ContainerEvents so I could just register myself as a listener but I want to know why I can't get the existing listener to work!
I looked at AWTEventListener with the mask ACTION_EVENT_MASK but I didn't receive any events! I am attaching my listener code.
I am attempting to build my own debugger/tester as a learning experience and need to monitor all occurances of various events. I am just starting and the first event I am having trouble with is action events. I have been able to determine whenever a button gets added using ContainerEvents so I could just register myself as a listener but I want to know why I can't get the existing listener to work!
final AWTEventListener eventMonitor = new AWTEventListener()
{
public void eventDispatched(AWTEvent e)
{
System.out.println("EM = " + e);
}
};
Toolkit.getDefaultToolkit().addAWTEventListener(eventMonitor, AWTEvent.ACTION_EVENT_MASK);
>> I have been able to determine whenever a button gets added using ContainerEvents
Then it must be easy to call
addActionListener(yourList ener);
on each added button.
Then it must be easy to call
addActionListener(yourList
on each added button.
ASKER
Hello zzynx!
Sure I could do that but then I am putting testing code all over my application that I then have to remove when I deploy...
From what I read of AWTEventListener it is supposed to by system wide so why does it not get ActionEvents from the many buttons in my application?
Sure I could do that but then I am putting testing code all over my application that I then have to remove when I deploy...
From what I read of AWTEventListener it is supposed to by system wide so why does it not get ActionEvents from the many buttons in my application?
ASKER
Again why do I have to listen for container add events when there is an ActionEvent mask? What do you have to do to get ActionEvents sent to the AWTEventListener?
As a last resort I can do this for buttons but I fear that will not solve other problems I will encounter later...
As a last resort I can do this for buttons but I fear that will not solve other problems I will encounter later...
>> Sure I could do that but then I am putting testing code all over my application that I then have to remove when I deploy...
Not if you do it in your ContainerListener code. See my previous comment.
Not if you do it in your ContainerListener code. See my previous comment.
In the java doc for the class ActionEvent I read:
A semantic event which indicates that a component-defined action occurred. This high-level event is generated by a component (such as a Button) when the component-specific action occurs (such as being pressed). The event is passed to every ActionListener object that registered to receive such events using the component's addActionListener method.
A semantic event which indicates that a component-defined action occurred. This high-level event is generated by a component (such as a Button) when the component-specific action occurs (such as being pressed). The event is passed to every ActionListener object that registered to receive such events using the component's addActionListener method.
ASKER
I read that too but in the AWTEventListener interface I read this:
"The listener interface for receiving notification of events dispatched to objects that are instances of Component or MenuComponent or their subclasses. Unlike the other EventListeners in this package, AWTEventListeners passively observe events being dispatched in the AWT, >>>system-wide<<<<. "
And what I am trying to do is mentioned in the last sentence "event recorders for automated testing".
Yet when I create an instance of this interface with the action mask I get no action events when I press buttons in my application?
BTW Thanks for your help on this...
"The listener interface for receiving notification of events dispatched to objects that are instances of Component or MenuComponent or their subclasses. Unlike the other EventListeners in this package, AWTEventListeners passively observe events being dispatched in the AWT, >>>system-wide<<<<. "
And what I am trying to do is mentioned in the last sentence "event recorders for automated testing".
Yet when I create an instance of this interface with the action mask I get no action events when I press buttons in my application?
BTW Thanks for your help on this...
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Sooo close...
I was able to detect if the source of the component was a button on mouse clicks; but now if I use the keyboard to activate the button I have to know which context is being displayed so that I can map the key to a button. Sometimes F1 means activate button X sometimes button Y, so unless I know which button is displayed when I capture the key event I don't know which button will be activated. What I really want to monitor is the firing of an ActionEvent...
So maybe the only way to do that is to follow your first suggestion, when I add buttons register the monitoring class as an action listener. I'll give it some more time...
I was able to detect if the source of the component was a button on mouse clicks; but now if I use the keyboard to activate the button I have to know which context is being displayed so that I can map the key to a button. Sometimes F1 means activate button X sometimes button Y, so unless I know which button is displayed when I capture the key event I don't know which button will be activated. What I really want to monitor is the firing of an ActionEvent...
So maybe the only way to do that is to follow your first suggestion, when I add buttons register the monitoring class as an action listener. I'll give it some more time...
ASKER
I was not able to detect ActionEvents like I wanted so the lower grade.
FTR I believe the ActionEvents are wrapped up inside of SequencedEvents or SentEvents but these classes can only accessed via reflection. Whenever I tried to write code that referenced them by name I would get errors.
FTR I believe the ActionEvents are wrapped up inside of SequencedEvents or SentEvents but these classes can only accessed via reflection. Whenever I tried to write code that referenced them by name I would get errors.
Thanx 4 axxepting
By doing
yourButton.addActionListen
for every button
with yourListener implementing the ActionListener interface