1
this.addWindowListener(new WindowAdaptor(){
public void windowClosing(WindowEvent we)
{
    System.exit(0);//to close the window
}
});

This is my request, please make the code using lambda expression.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Manohar
  • 35
  • 1
  • 3

2 Answers2

4

A lambda expression can substitute a functional interface (i.e. an interface with a single non default method). Therefore WindowAdapter, which has multiple methods (windowActivated(WindowEvent e), windowClosed(WindowEvent e), windowClosing(WindowEvent e), ...), can't be substituted by a lambda expression.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    Short reference about lambdas and functional interfaces: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#approach5 – Pshemo May 15 '15 at 13:57
1

Directly you cannot use lambdas here, but if you have pretty much UI code where you have plenty of listeners, you may create a builder like this:

import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.function.Consumer;

public class ListenerBuilder {
    static final Consumer<WindowEvent> nullConsumer = e -> {}; 

    private Consumer<WindowEvent> opened = nullConsumer;
    private Consumer<WindowEvent> closing = nullConsumer;
    private Consumer<WindowEvent> closed = nullConsumer;
    private Consumer<WindowEvent> iconified = nullConsumer;
    private Consumer<WindowEvent> deiconified = nullConsumer;
    private Consumer<WindowEvent> activated = nullConsumer;
    private Consumer<WindowEvent> deactivated = nullConsumer;

    public ListenerBuilder opened(Consumer<WindowEvent> opened) 
    { this.opened = opened; return this; }

    public ListenerBuilder сlosing(Consumer<WindowEvent> closing) 
    { this.closing = closing; return this; }

    public ListenerBuilder closed(Consumer<WindowEvent> closed) 
    { this.closed = closed; return this; }

    public ListenerBuilder iconified(Consumer<WindowEvent> iconified) 
    { this.iconified = iconified; return this; }

    public ListenerBuilder deiconified(Consumer<WindowEvent> deiconified) 
    { this.deiconified = deiconified; return this; }

    public ListenerBuilder activated(Consumer<WindowEvent> activated) 
    { this.activated = activated; return this; }

    public ListenerBuilder deactivated(Consumer<WindowEvent> deactivated) 
    { this.deactivated = deactivated; return this; }

    public WindowListener build() {
        return new WindowListener() {
            public void windowOpened(WindowEvent e) {
                opened.accept(e);
            }
            public void windowIconified(WindowEvent e) {
                iconified.accept(e);
            }
            public void windowDeiconified(WindowEvent e) {
                deiconified.accept(e);
            }
            public void windowDeactivated(WindowEvent e) {
                deactivated.accept(e);
            }
            public void windowClosing(WindowEvent e) {
                closing.accept(e);
            }
            public void windowClosed(WindowEvent e) {
                closed.accept(e);
            }
            public void windowActivated(WindowEvent e) {
                activated.accept(e);
            }
        };
    }

    public void attachTo(Window w) {
        w.addWindowListener(build());
    }
}

After that it can be used like this:

this.addWindowListener(new ListenerBuilder()
    .opened(e -> log.info("Window is opened!"))
    .closed(e -> System.exit(0)).build());

Or like this:

new ListenerBuilder()
    .opened(e -> log.info("Window is opened!"))
    .closed(e -> System.exit(0)).attachTo(this);
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • 1
    The drawback of this design is that it silently overwrites a handler if a method is called more than once. Further, the generated listener references the handlers though the builder instance which makes it mutable and the entire usage of the Builder pattern pointless. – Holger May 18 '15 at 11:11
  • Despite the simple example implementation, the idea of using a builder here is great. Lombok can generate a builder for you. The auto-generated builder fixes all the mentioned drawbacks. – Socowi Oct 16 '20 at 07:33