I'm tryig to make a lambda expresion for an ActionListener, but it's giving me an IllegalStart of expression, what i'm trying to run so far looks like this:
JFrame frame = new JFrame();
JButton boton = new JButton("Lambda Button");
boton.addActionListener(event -> System.out.println("Hello World!"));
frame.add(boton);
frame.setVisible(true);
On the other hand, when i use this code instead:
JFrame frame = new JFrame();
JButton boton = new JButton("Lambda Button");
boton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello World!");
}
} );
frame.add(boton);
frame.setVisible(true);
It works perfectly fine,
Initially i tought the issue could be the version of java i'm running, but i just updated and keeps doing the same, when i do a java -version i gives me the following:
java -version java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode)
So, as far as i know it have a version compatible with lambda expression but not succed on making them work, any ideas or suggestion of what could he happening ?
EDIT: When i try to compile i get this:
Prueba.java:57: error: illegal start of expression
boton.addActionListener(event -> System.out.println("Hello World !"));
^1 error
EDIT2: I'm not using any IDE, im compiling from the command line