wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to fire events ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to fire events ?" Watch "How to fire events ?" New topic
Author

How to fire events ?

Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
Hi All,

Is it possible to fire system events like ActionEvent or MouseEvent ?

Most of the constructors of these events require the event source and id of the event.

So lets say in some simulation project, I need a particular button to be clicked n number of times at m intervals.
So can I create an ActionEvent object with that button as source and then somehow fire the event at regular intervals instead of manually having to press the button.

Gopals.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

You should be able to call button.doClick() which should fire the ActionEvent associated with the JButton.


GenRocket - Experts at Building Test Data
Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
Thanks Greg for the reply. But I think I was looking for a different answer.

I was trying to understand the meaning of an event.

I got the definition of an event as:

Events are objects or messages used when a software components wants to notify a state change to other components.


As per my understanding the classification can be of 3 types.

1. System generated GUI events (All the events which inherit from java.awt.AWTEvent class as of JDK1.1
2. System generated non-gui events. I am really not sure how this is possible or don't have an example for this. But from the design perspective this shud be possible.
Let me guess something like a task manager.
3. User generated events. This can be a Event Source implementing observer design pattern in which event handlers can register with the event source for notification.

If this classification is correct, then do we follow any standards for all the three categories of events.

I mean all exceptions have a common root, all threads have a common root, all objects have a common root. So why not a common root for all kinds of events ?

There should be a standard mechanism for creation as well as firing of events.

Secondly, if system generated GUI events r something which should have been generated only by System, then why do we have public constructors for all such events. :roll:

I was again thinking over the difference between creating an instance of event and firing of an event.
What can a user do by creating an instance of a system event, if he cannot fire it ? or is there any way to fire the event.

Thanks in advance,
Gopals.
Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
I found an example of non-gui events.

JSSE (Java Secure Sockets Extension), uses the standard event model.

a) HandshakeCompletedListener extends java.util.EventListener
b) HandshakeCompletedEvent extends java.util.EventObject

The source i.e. a particular SSLSocket object will add the event handler using addHandshakeCompletedListener() and remove using removeHandshakeCompletedListener().

But I still don't have an idea as how the event will be fired and how the handler gets the notification ?

Does it mean all the event objects have some private methods to notify which when invoked notifies the corresponding event handler object ?

Regards,
Gopals.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
There is a common class parent of all the Events in the JDK 1.0 Event Model. It is java.awt.Event. This model is obsolete. You can read read about it here thanks to R.G.Baldwin. You can also read about the advantages of the new Delegation Event Model in JDK 1.1 here.
The common class for all the events in the Delegation Event Model is java.util.EventObject.

The use of Observer/Observable in Java to implement the publish-subscribe pattern is dated in JDK 1.0. A that time the DEM model was not out. DEM goes beyond MVC because encapsulates what has changed in an Object: the event. And we have a hierarquy of events to express different events.

The constructors are public because they need to be called from outside the package they were declared.

The short answer is that you have the ability to contruct and fire the events you need to do so; while others events are "automatically" created and managed:
Low level events represent interactions of the user with the window, mouse or keyboard. The OS passes messages for this interaction to the Java runtime who contructs and fires the apropiate events.
Semantic events has meaning regardless how they were spawned. When an ActionEvent signals that a button was pressed you are interested in the meaning of that hit for the program, not in knowing if the mouse or keyboard was involved. Some of the semantic events can be generated without user interaction, they can be generated programatically via the apropiate methods. Like AbstractTableModel.fireTableXXX


SCJP2. Please Indent your code using UBB Code
Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
Thanks Jose for the explanation. I now have got the answer to my questions. For all AWTEvents, the general procedure to fire events is calling processEvent method, which is supported by all the AWT & Swing Components. I tried it in my code and it worked. The source is provided below.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to fire events ?