This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi, 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!!! Barry
Barry, 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:
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.