Creating Events using Adapter classes

The adapter class approach is slightly simpler than the interface approach so I will only cover that for the moment. Here is a modified version of HelloWorld that includes an adapter class that allows you to close down the Frame gracefully without resorting to the two finger salute, or Ctrl-c.


/* ShutHello */
// A Single line comment
import javax.swing.*;
import java.awt.event.*;//Note that handling is an awt class
public class ShutHello extends JFrame{
    public static void main(String argv[]){
	ShutHello shuthello = new ShutHello();
	shuthello.go();
    }
    public void go(){
	//Create an instance of the Adapter class
	WindowCloser wc = new WindowCloser();
	//attach that class to the current class
	addWindowListener(wc);
	JButton button = new JButton("Hello");
	getContentPane().add(button);
	setSize(100,100);	
	setVisible(true);

    }
}
     
class WindowCloser extends WindowAdapter{
        //override one of the methods in the Adapter class
        public void windowClosing(WindowEvent e){
        System.exit(0);
        }
}

The event handling code

        WindowCloser wc = new WindowCloser();
        //Attach that listener to this program
        addWindowListener(wc);

The first line creates an instance of the WindowCloser class (explained a little later here). The next line attaches this class as a listener to the current instance of the program. This line has an implicit reference to the this instance. Thus it would also correct to have this line as

this.addWindowListener(wc);

If the WindowCloser class were correctly defined outside this file, this would be all you would have to do to implement code to close this window. However, in this instance the WindowCloser class is actually defined within the same file and is explained next.

class WindowCloser extends WindowAdapter{

This line defines a new class within the current file. The name WindowCloser is arbitrary but I have chosen it to indicate the purpose of the class. It extends WindowAdapter which is a class that offers methods that control the sort of events that might happen to a window.

        public void windowClosing(WindowEvent e){
        System.exit(0);
        }

These lines override the windowClosing method in the WindowAdapter class. Note the naming convention where the method name starts with a lower case letter. This is in contrast with the name of the parameter that starts with an upper case letter. When learning the methods it is easy to get this convention wrong and wonder why the code will compile but not do what you expect. With luck you will mainly use a GUI builder to actually create this sort of code.

The System.exit(0) line simply halts the running of the current program. It would be possible to write code to close the current window, but stopping the current program is easy to do and has the effect of closing the window. This line that shuts down the program is where the real work is done. The rest of the code is mainly window dressing. There are a limited number of events that can be trapped and it is what you do with them that counts. Thus for instance you might attach an event to a button that opens another frame and initialises some values on that frame according to the contents of a text field.

If you use a GUI builder such as NetBeans you will soon notice if you look at the code that it does not produce external classes for the event handling code as I have but uses the concept of inner classes. Where I have created the WindowCloser class as an external class within the same file, JBuilder creates an instance of the class actually within the event handling method. This is very handy for a GUI builder tool and keeps the code compact but it is more complex to write by hand as you have to keep a close watch on opening and closing of parenthesis and curly braces. The actual principal of using Listeners is identical.

Last modified: Thursday, 24 July 2014, 2:54 PM