Skip to content

Instantly share code, notes, and snippets.

@shnplr
Last active August 29, 2015 14:22
Show Gist options
  • Save shnplr/32b2720f5ba3ad27d963 to your computer and use it in GitHub Desktop.
Save shnplr/32b2720f5ba3ad27d963 to your computer and use it in GitHub Desktop.
Problem with Cursor WAIT binding and ComboBox
package sample;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.concurrent.Task;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Steps to reproduce issues:
* 1. Select "DEV" from first dropdown - it will run for 20s
* 2. Immediately select "TEST" from 2nd dropdown - it will run for 5s
*
* Expected result
* 1. Cursor remains WAIT until both tasks have completed
* 2. When both tasks have completed Cursor should remain at DEFAULT
* when placed over 2nd comboxbox drop down
*
* Actual result
* 1. Cursor changes to DEFAULT before 1st task has completed
* 2. (Intermittent): After both tasks have completed, clicking on 2nd combo
* and placing mouse pointer over dropdown list, causes cursor to change
* back to WAIT.
*/
public class ComboSample extends Application {
public static void main(String[] args) { launch(args); }
private final ExecutorService pool = Executors.newFixedThreadPool(4);
final ObjectProperty<Cursor> CURSOR_DEFAULT = new SimpleObjectProperty<>(Cursor.DEFAULT);
final ObjectProperty<Cursor> CURSOR_WAIT = new SimpleObjectProperty<>(Cursor.WAIT);
@Override public void start(final Stage primaryStage) {
primaryStage.setTitle("Combo Sample");
ComboBox<String> from = new ComboBox<>();
from.getItems().addAll("DEV", "TEST", "PROD");
from.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
Task<Void> task = new Task<Void>() {
@Override protected Void call() throws Exception {
System.out.println("selectFrom - Start");
Thread.sleep(20000);
System.out.println("selectFrom - End");
return null;
}
};
primaryStage.getScene().cursorProperty().bind(Bindings.when(task.runningProperty())
.then(CURSOR_WAIT).otherwise(CURSOR_DEFAULT));
pool.submit(task);
});
ComboBox<String> to = new ComboBox<>();
to.getItems().addAll("DEV", "TEST", "PROD");
to.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
System.out.println("selectTo - Start");
Thread.sleep(5000);
System.out.println("selectTo - End");
return null;
}
};
primaryStage.getScene().cursorProperty().bind(Bindings.when(task.runningProperty())
.then(CURSOR_WAIT).otherwise(CURSOR_DEFAULT));
pool.submit(task);
});
HBox layout = new HBox(10);
layout.getChildren().addAll(from, to);
primaryStage.setScene(new Scene(layout));
primaryStage.setWidth(400);
primaryStage.setHeight(200);
primaryStage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment