• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to fire events ?

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be able to call button.doClick() which should fire the ActionEvent associated with the JButton.
 
Gopal Shah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Gopal Shah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic