6.06 - Adding Window Events

At this point we have out application working, but the what if we would like the application to quit when we press the 'X' in the top right-hand corner of the window? Well we need to look at Window Events.

If we look at the class Frame in the Java API documentation we can see that it has multiple parent classes. The API describes it as:

Class Frame

This means that any method that is available in the Window class or the Container class etc. is also present in the Frame class. If we read more in the API documentation we can see that:

Frames are capable of generating the following types of WindowEvents:

    If the program doesn't explicitly hide or dispose the window while processing this event, the window close operation is canceled.

So, the events are associated with the Window class (as they are WindowEvents) and if we go down we can see:

That we should look at the addWindowListener() method, which expects an object that has implemented the WindowListener interfaces. This interface is described as having the following methods that you must implement:

Modifier and TypeMethod and Description
voidwindowActivated(WindowEvent e)
Invoked when the Window is set to be the active Window.
voidwindowClosed(WindowEvent e)
Invoked when a window has been closed as the result of calling dispose on the window.
voidwindowClosing(WindowEvent e)
Invoked when the user attempts to close the window from the window's system menu.
voidwindowDeactivated(WindowEvent e)
Invoked when a Window is no longer the active Window.
voidwindowDeiconified(WindowEvent e)
Invoked when a window is changed from a minimized to a normal state.
voidwindowIconified(WindowEvent e)
Invoked when a window is changed from a normal to a minimized state.
voidwindowOpened(WindowEvent e)
Invoked the first time a window is made visible.
So, let us modify our previous code so that the when the window activates that the scroll value is set back to 50 and when the 'X' in the top right-hand side is clicked that the program exits.

So, we want to implement the WindowListener interface on our class. Unfortunately, we have to implement all 7 methods or our code will not compile. So, we will add custom code for windowClosing() to exit our application and custom code for windowActivated() to set the value to 50 every time you activate the window (e.g. maximising after minimising). The code looks like this:

package ee402;

import java.awt.*;
import java.awt.event.*;

public class ScrollbarExampleEvents extends Frame implements AdjustmentListener, ActionListener, WindowListener
        private Scrollbar scrollbar;
        private TextField status;
        private Button resetButton;

        public ScrollbarExampleEvents(){
                super("Scrollbar Example");
                this.setLayout(new GridLayout(2,1));
                Panel topPanel = new Panel();

                this.status = new TextField(20);
                this.scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 110);
                this.resetButton = new Button("Reset");





        public void actionPerformed(ActionEvent e){
                if (e.getActionCommand().equals("Reset")){

        public void adjustmentValueChanged(AdjustmentEvent e){
                if (e.getSource().equals(scrollbar)){

        private void updateText(){
                status.setText("Scroll Value = " + scrollbar.getValue());

        public static void main(String[] args) {
                new ScrollbarExampleEvents();

        public void windowActivated(WindowEvent arg0) {
        public void windowClosed(WindowEvent arg0) {}
        public void windowClosing(WindowEvent arg0) {
        public void windowDeactivated(WindowEvent arg0) {}
        public void windowDeiconified(WindowEvent arg0) {}
        public void windowIconified(WindowEvent arg0) {}
        public void windowOpened(WindowEvent arg0) {}

Now if you use the 'X' on the top right-hand corner the program exits and if you minimise and then maximise you will see the value reset to 50.

These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
Derek Molloy,
13 Nov 2013, 03:38