In: Computer Science
1. Explain how two controls can be set up to share the same event handler. How can the event handler tell which control generated the event?
2. Which user action generates three separate mouse events? Which events? Why?
3. Compare and contrast event handlers and change listeners.
Hello, I would really appreciate it if these 3 questions could be explained to me. These are in reference to the JAVA programming language. Specifically the JAVA GUI. I greatly appreciate a clear and understandable explanation. I WILL NOT BE COPY AND PASTING YOUR ANSWERS. Instead, I want to learn so I can form it in my own words.
ANSWER:---
1)---
Event:--
Change in the state of an object is known as event i.e. event describes the change in state of source. the system produces (or "fires") a signal of some kind when an event occurs, and provides a mechanism by which an action can be automatically taken when the event occurs.
Event Handling:-----
Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs.
Java Uses the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events.
The java.awt.event package provides many event classes and Listener interfaces for event handling.
Delegation Event Model has the following key :--
Steps involved in event handling:----
The User clicks the button and the event is generated.
· Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object.
· Event object is forwarded to the method of registered listener class.
· the method is now get executed and returns.
2)------
For example, clicking a mouse button generates three events, a "mouse pressed" event, a "mouse released" event, and a "mouse clicked/typed" event.
the general form of these methods are void KeyPressed(KeyEventke), void KeyReleased(KeyEvent ke),, void KeyTyped(KeyEvent ke)
Simply moving the mouse generates a sequence of events as the mouse cursor moves from point to point on the screen. To respond to mouse events on a component, you can register listeners with that component.
We could register separate listener for each kind of mouse event on a component c using instance methods such as c.setOnMousePressed(handler) and c.setOnMouseMoved(handler).
3) ----------
Change listener |
Event handler |
A listener watches for an event to be fired. For example, a KeyListener waits for KeyEvents, a MessageListener waits for messages to arrive on a queue and so on. |
The handler is responsible for dealing with the event. Normally, listeners and handlers go hand-in-hand. For example, the KeyListener tells the ExitHandler that "the letter Q was pressed" and the handler performs logic such as cleaning up resources and exiting the application gracefully. Similary a ButtonClickListener would tell the same ExitHandler that the "Exit button was clicked". So, in this case you have two different events, two different listeners but a single handler. |
A listener, listens for events which are data value objects which describe an event. When the event occurred and the order of events is often important. Pressing key '0' followed by '1' is different to '1' and '0'. |
A handler, handles a complex object e.g. a new Socket connection. The handler might process the object for any length of time. The time of object creation and order is not so important. A connection from client0 or client1 can happen in any order. |
Listener is associated with Event Source (Ex: key board) |
Handler is associated with an Event (Ex: keydown |
EventHandler is introduced in the JavaFX for all the UI controls. The EventHandler is a way to distinguish observable events and the ui events. |
Whereas the Listener is borrowed for Observables, such as properties. |
A listener, listens for events which are data value objects which describe an event. |
A handler, handles a complex object. It is responsible for dealing with the event |
"listeners" at the more abstract UI level (where you'll be implementing your application logic). |
handlers" at the look-and-feel level (for handling low-level widget events) |
,