Java Reference
In-Depth Information
Those defined in the FocusEvent class are:
FOCUS _ GAINED
FOCUS _ LOST
To implement a listener for a particular event type you just need to implement the methods declared in
the corresponding interface. We could handle some of the window events for our SketchFrame class
by making the application class the listener for window events.
Try It Out - Implementing a Low-level Event Listener
First, delete the call to the enableEvents() method in the SketchFrame() constructor. Then
delete the definition of the processWindowEvent() method from the class definition.
Now we can modify the Sketcher class so that it is a listener for window events:
// Sketching application
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
public class Sketcher implements WindowListener {
public static void main(String[] args) {
theApp = new Sketcher(); // Create the application object
theApp.init(); // ...and initialize it
}
// Initialization of the application
public void init() {
window = new SketchFrame("Sketcher"); // Create the app window
Toolkit theKit = window.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size
// Set the position to screen center & size to half screen size
window.setBounds(wndSize.width/4, wndSize.height/4, // Position
wndSize.width/2, wndSize.height/2); // Size
window.addWindowListener(this); // theApp as window listener
window.setVisible(true); // Display the window
}
// Handler for window closing event
public void windowClosing(WindowEvent e) {
window.dispose(); // Release the window resources
System.exit(0); // End the application
}
// Listener interface functions we must implement - but don't need
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
Search WWH ::




Custom Search