We help IT Professionals succeed at work.

Universal Java ActionListener

macgre
macgre asked
on
545 Views
Last Modified: 2013-12-13
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!
final AWTEventListener eventMonitor = new AWTEventListener()
{
   public void eventDispatched(AWTEvent e) 
   {
	System.out.println("EM = " + e);
   }
};
		
Toolkit.getDefaultToolkit().addAWTEventListener(eventMonitor, AWTEvent.ACTION_EVENT_MASK);

Open in new window

Comment
Watch Question

zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> How do I create a single listener that will be informed of every button press
By doing

yourButton.addActionListener(yourListener);

for every button

with yourListener implementing the ActionListener interface
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> I have been able to determine whenever a button gets added using ContainerEvents
Then it must be easy to call

  addActionListener(yourListener);

on each added button.

Author

Commented:
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?

Author

Commented:
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...
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> 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.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
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...
Sr. Software engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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...

Author

Commented:
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.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Thanx 4 axxepting
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.