Java window events
Created By: chatGPT
Java provides a robust event-handling mechanism that allows programmers to respond to various actions performed by users or the system. Events can be associated with mouse actions, keyboard inputs, and other user interface interactions. In Window events specifically, we can handle actions such as closing a window, resizing it, or activating it.
import javax.swing.*;
import java.awt.event.*;
public class WindowEventExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Window Event Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("Window is closing.");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("Window is activated.");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("Window is deactivated.");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("Window is minimized.");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("Window is restored.");
}
});
frame.setVisible(true);
}
}
In the code above, we create a simple JFrame window and attach a WindowListener to it using an anonymous class that extends WindowAdapter. This allows us to override specific methods for handling different window events without implementing all methods in the WindowListener interface. We handle events such as closing, activating, and minimizing the window.
class CustomWindowListener extends WindowAdapter {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("Window opened.");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("Window closed.");
}
}
To further extend our example, we can create a separate class that implements the WindowListener interface fully. This allows for more organized code, especially if multiple methods need to be managed. Each method can be defined, giving you full control over what happens during a specific window event.
public class CustomWindow implements WindowListener {
@Override
public void windowActivated(WindowEvent e) {
System.out.println("Window activated.");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("Window deactivated.");
}
// Implement other WindowListener methods
@Override
public void windowOpened(WindowEvent e) {}
@Override
public void windowClosing(WindowEvent e) {}
@Override
public void windowClosed(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {}
@Override
public void windowDeiconified(WindowEvent e) {}
}
This structured approach makes it simpler to manage the flow of the program and keeps the event-response logic neatly organized. Remember, when implementing listeners, always ensure to handle event unbinding if the created window is no longer needed in order to free resources and avoid memory leaks. Understanding window events is crucial for developing a responsive and user-friendly application in Java.
frame.addWindowListener(new CustomWindow());