I have a virtual machine running Windows XP SP3 32-bit. On this machine I installed the Java SE JDK 8 build b44 Developer Preview from here.
I also installed the JavaFX 2.1 SDK.
It works fine:
java -version
> java version "1.8.0-ea"
> Java(TM) SE Runtime Environment (build 1.8.0-ea-b44)
> Java HotSpot(TM) Client VM (build 24.0-b14, mixed mode, sharing)
I tried running the following program (taken from here):
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleButtonBuilder;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class LambdasWithJavaFx extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override public void start(Stage stage) throws Exception
{
BorderPane root = new BorderPane();
ToggleButton button = new ToggleButton("Click");
final StringProperty btnText = button.textProperty();
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent actionEvent)
{
ToggleButton source = (ToggleButton) actionEvent.getSource();
if (source.isSelected())
{
btnText.set("Clicked!");
}
else
{
btnText.set("Click!");
}
}
});
root.setCenter(button);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setWidth(200);
stage.setHeight(200);
stage.show();
}
}
The program compiled and ran as expected.
I followed the instructions in that article and replaced the button event-handling code with this:
button.setOnAction((ActionEvent event)->
{
ToggleButton source = (ToggleButton) event.getSource();
if (source.isSelected())
{
btnText.set("Clicked!");
}
else
{
btnText.set("Click!");
}
});
When compiling, I get the following error (on the line button.setOnAction((ActionEvent event)->
):
> lambda expressions are not supported in -source 1.8
> (use -source 8 or higher to enable lambda expressions)
I added the argument -source 8
, nothing changed.
All I wanted was to check the lambda expressions functionality in Java 8. Why doesn't it work ?