JavaFX using JDK1.8 Lambda Expression
This is 21th of the JavaFX Advent Calendar (see JavaFX Advent Calendar 2012 : ATND for other articles)
This is my first article in English so if you find any mistakes feel free to give me comments.
I use Mac OS X 10.7.4 and JDK1.8.0
The following shows the screen I write with lambda expression and javafx concurrent task.
- Start
- Progressing
Click the Start button to start the task.The Progress Bar fills as the operation progresses and the text message continually updates to indicate the percentage of progress completed.
- Done and Cancelled
Click the Cancel button to cancel the task.
Below is the lambda expression I write in this example.
- for Task1
It is possible to use lambda expression to set action or add event handler
EventHandler<ActionEvent> sHandler = e -> { btn1.setDisable(true); btnC1.setDisable(false); task1 = new ProgressTask("Task1", 10); bar1.progressProperty().bind(task1.progressProperty()); text1.textProperty().bind(task1.messageProperty()); service.submit(task1); EventHandler<WorkerStateEvent> tHandler = ee -> {btn1.setDisable(false); btnC1.setDisable(true);}; task1.addEventHandler( WorkerStateEvent.WORKER_STATE_SUCCEEDED, tHandler); task1.addEventHandler( WorkerStateEvent.WORKER_STATE_CANCELLED, tHandler); }; btn1.setOnAction(sHandler); EventHandler<ActionEvent> cHandler = e -> {task1.cancel();}; btnC1.setOnAction(cHandler);
- for Task2
Implementation almost same as task 1, but use lambda expression in parameter
btn2.setOnAction(e -> { btn2.setDisable(true); btnC2.setDisable(false); task2 = new ProgressTask("Task2", 20); bar2.progressProperty().bind(task2.progressProperty()); text2.textProperty().bind(task2.messageProperty()); service.submit(task2); task2.addEventHandler( WorkerStateEvent.WORKER_STATE_SUCCEEDED, ee -> {btn2.setDisable(false); btnC2.setDisable(true);} ); task2.addEventHandler( WorkerStateEvent.WORKER_STATE_CANCELLED, ee -> {btn2.setDisable(false); btnC2.setDisable(true);} ); }); btnC2.setOnAction(e -> {task2.cancel();});
- for Task
There is no lambda expression.
I want to update message when the task cancelled then extend the Task and Override cancelled method.
public class ProgressTask extends Task<String> { private String taskName; private int numberOfTask; public ProgressTask(String taskName, int numberOfTask) { this.taskName = taskName; this.numberOfTask = numberOfTask; } @Override protected String call() throws Exception { updateMessage("started"); int i; for (i = 1; i <= numberOfTask; i++) { if (isCancelled()) { updateMessage("??? cancelled ???"); // this dose not work??? break; } updateProgress(i, numberOfTask); TimeUnit.SECONDS.sleep(1); updateMessage(String.format("running %d/%d", i, numberOfTask)); } updateMessage("done"); return "Done"; } @Override protected void updateMessage(String string) { super.updateMessage(taskName + " " + string); } @Override protected void cancelled() { updateProgress(0, numberOfTask); updateMessage("cancelled"); super.cancelled(); } }
I hope this helps you write JavaFX with lambda expressions as well as javafx concurrent task.
I put all sources to github.(see tomoTaka01/progresssample: JavaFX using JDK... - GitHub)
Have a nice holiday!