Window Events with Event Frame

The code for our example is pretty simple. In the constructor of the EventFrame class, we register for every event that Frame publishes. All of the events are registered to the same event handler method,

#handles all of the events in this demo def _handle_event(self, event):


The event handler calls self. event_viewer . addEvent, which adds the event to a listbox so we can view it. addEvent () forces the event to the top of the list, using back quotes to get its string representation.

def addEvent(self, event):

Now let's use Event Frame to examine all of the events that the frame handles:

• windowActivated

• windowClosed

• windowClosing

• windowDeactivated

• windowDeiconified

• windowlconified

• windowOpened

To use WindowActivated, fill we need to do Wnen the window comes up, we get the following events:

• java.awt.event.WindowEvent[WINDOW_ACTIVATED]on frame0

• java.awt.event.FocusEvent[FOCUS_GAINED,permanent] on frame0

• j ava.awt.event.ComponentEvent[COMPONENT_SHOWN] on frame0

• j ava.awt.event.WindowEvent[WINDOW_OPENED] on frame0

WindowEvent, with its ID set to WINDOW_ACTIVATED, corresponds to the windowActivated event property of the frame. This pattern repeats itself for all event properties and event IDs. Notice that, in addition to windowOpened and windowActivated,Frame publishes focusGained and componentShown.

When the focus changes to another window, you get the following wi ndowDe activated events. Click on the caption of the event viewer

• j ava.awt.event.WindowEvent[WINDOW_DEACTIVATED] on frame0

• java.awt.event.FocusEvent[FOCUS_LOST,temporary] on frame0

To get a windowlconified event, we have to minimize the window by clicking the Minimize button on the right side of the window's caption. The following events result:

• java.awt.event.FocusEvent[FOCUS_LOST,temporary] on frame0

• j ava.awt.event.WindowEvent[WINDOW_ICONIFIED] on frame0

• j ava.awt.event.WindowEvent[WINDOW_DEACTIVATED] on frame0

Now we want to restore tha frame, so we go to the taskbar and click the Frames icon. These are the wents we

• j ava.awt.event.WindowEvent[WINDOW_ACTIVATED] on frame0

• j ava.awt.event.WindowEvent[WINDOW DEICONIFIED] on frameQ

• java.awt.event.FocusEvent[FOCUS_GAINED,permanent] on frameO Then we click tie window's Close button to get tie windowClosing event:

• j ava.awt.event.WindowEvent[WINDOW_CLOSING] on frame0

The event is a request to close, rot tie actual closing. For that we have to restart the application in the interactive interpreter trY type

j ava.awt.event.ComponentEvent[COMPONENT_HIDDEN] on frame0 >>> frame.dispose()

java.awt.event.WindowEvent[WINDOW_CLOSED] on frame0 Hiding the frame doesn't close it The dispose ( ) function does that.

Was this article helpful?

0 0

Post a comment