WindowListener vs WindowAdapter

WindowListener vs WindowAdapter


WindowListener vs WindowAdapter
We know earlier difference between a listener and adapter. Now let us dig for WindowListener vs WindowAdapter.

First let us discuss interms of API WindowListener vs WindowAdapter.

Following are the signatures as defined in Java API.

1. public interface WindowListener extends EventListener

2. public abstract class WindowAdapter implements WindowListener, WindowStateListener, WindowFocusListener

WindowListener is an interface and WindowAdapter is an abstract class.

There exists 7 abstract methods in WindowListener.

public abstract void windowOpened(WindowEvent);
public abstract void windowClosing(WindowEvent);
public abstract void windowClosed(WindowEvent);
public abstract void windowIconified(WindowEvent);
public abstract void windowDeiconified(WindowEvent);
public abstract void windowActivated(WindowEvent);
public abstract void windowDeactivated(WindowEvent);

Observe, the WindowAdapter implements WindowListener. All the above 7 abstract methods of WindowListener are overridden by WindowAdapter.

If you implement WindowListener you are forced to override all the 7 abstract methods. Infact, you will be using only one method windowClosing(WindowEvent). Atleast empty body should be given for all the remaining 6 methods. To overcome this problem of overriding all WindowAdapter was introduced.

So, now instead of implementing WindowListener, you extend WindowAdapter. Now override any one or two methods you like. This is the advantage of adapters over listeners.

Now, WindowAdapter makes window (frame) closing code easier and cleaner. It is because you override any one abstract method you require. It is generally windowClosing(WindowEvent e).

WindowListener is preferred only when you override many of the 7 abstract methods. Else WindowAdapter is the preferred choice.

Generally WindowAdapter is used in WindowListener vs WindowAdapter.

Leave a Reply

Your email address will not be published. Required fields are marked *