File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes firing an event when it has not been fired Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "firing an event when it has not been fired" Watch "firing an event when it has not been fired" New topic

firing an event when it has not been fired

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Does anyone know of a way to hook up let's say an ActionListener to a Button, and then have that event fire automatically even though noone has clicked it? I guess this seems like a strange thing to do, but I need to know if I can do this. Some sample code would help. Thanks in advance!!!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
I have not yet found out how to do it nicely. What I do in the short term is to override the component and handle the event myself. What this allows me to do is to call the processEvent method of my component. All AWT components have protected access to their processEvent methods; therefore, I can't call them unless I extend the components. I usually override the processEvent method with a public accessor. That way I won't have any trouble calling it from whereever I like ...
Here is a simple example showing the concept:

Hope this helps,
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
It may be a cheezy work-around, but the way I do this is by using anonymous inner-classes. I use adapter classes as event listeners for my components. Inside of these inner classes I call a method specific to that event and that component. In inner class I get the members of the event object and pass them as parameters to the method that actually handles the event. That way if I need to call that event I can just call it with parameters and I don't have to create an event object. So if I want to force that event to occur, I can just call the method that I want and off I go.
I agree. Here's the link:
subject: firing an event when it has not been fired
It's not a secret anymore!