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:
java.awt

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:

  • WINDOW_OPENED
  • WINDOW_CLOSING: 
    If the program doesn't explicitly hide or dispose the window while processing this event, the window close operation is canceled.
  • WINDOW_CLOSED
  • WINDOW_ICONIFIED
  • WINDOW_DEICONIFIED
  • WINDOW_ACTIVATED
  • WINDOW_DEACTIVATED
  • WINDOW_GAINED_FOCUS
  • WINDOW_LOST_FOCUS
  • WINDOW_STATE_CHANGED

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:

Methods 
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:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
package ee402;

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

@SuppressWarnings("serial")
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.addWindowListener(this);
                
                this.status = new TextField(20);
                this.scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 110);
                this.resetButton = new Button("Reset");

                this.resetButton.addActionListener(this);
                this.scrollbar.addAdjustmentListener(this);

                topPanel.add(status);
                topPanel.add(resetButton);

                this.add(topPanel);
                this.add(scrollbar);

                this.updateText();
                this.pack();
                this.setVisible(true);
        }

        public void actionPerformed(ActionEvent e){
                if (e.getActionCommand().equals("Reset")){
                        this.scrollbar.setValue(0);
                        this.updateText();
                }
        }

        public void adjustmentValueChanged(AdjustmentEvent e){
                if (e.getSource().equals(scrollbar)){
                        this.updateText();
                }
        }

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

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

        public void windowActivated(WindowEvent arg0) {
                this.scrollbar.setValue(50);
                this.updateText();
        }
        public void windowClosed(WindowEvent arg0) {}
        public void windowClosing(WindowEvent arg0) {
                System.exit(0);
        }
        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.
ċ
ScrollbarExampleEvents.java
(2k)
Derek Molloy,
13 Nov 2013, 03:38
Comments