Java Stream How to - Handle Swing action with lambda








Question

We would like to know how to handle Swing action with lambda.

Answer

//www.  ja va 2s.c om
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setVisible(true);

    JButton button1 = new JButton("");
    JButton button2 = new JButton("");

    frame.getContentPane().add(button1);
    frame.getContentPane().add(button2);

    button1.addActionListener(e -> {
      System.out.println("Lambda");
    });

    button2.addActionListener(Main::doSomething);

  }

  public static void doSomething(ActionEvent e) {
    System.out.println("");
  }
}