Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.beans.binding.Bindings;
  3. import javafx.beans.property.ObjectProperty;
  4. import javafx.beans.property.SimpleObjectProperty;
  5. import javafx.concurrent.Task;
  6. import javafx.scene.Cursor;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.ComboBox;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.layout.HBox;
  11. import javafx.scene.layout.VBox;
  12. import javafx.stage.Stage;
  13.  
  14. import java.util.concurrent.ExecutorService;
  15. import java.util.concurrent.Executors;
  16.  
  17. /**
  18. * Steps to reproduce issues:
  19. * 1. Select "DEV" from first dropdown - it will run for 20s
  20. * 2. Immediately select "TEST" from 2nd dropdown - it will run for 5s
  21. *
  22. * Expected result
  23. * 1. Cursor remains WAIT until both tasks have completed
  24. * 2. When both tasks have completed Cursor should remain at DEFAULT
  25. * when placed over 2nd comboxbox drop down
  26. *
  27. * Actual result
  28. * 1. Cursor changes to DEFAULT before 1st task has completed
  29. * 2. (Intermittent): After both tasks have completed, clicking on 2nd combo
  30. * and placing mouse pointer over dropdown list, causes cursor to change
  31. * back to WAIT.
  32. */
  33. public class ComboSample extends Application {
  34. public static void main(String[] args) { launch(args); }
  35.  
  36. private final ExecutorService pool = Executors.newFixedThreadPool(4);
  37. final ObjectProperty<Cursor> CURSOR_DEFAULT = new SimpleObjectProperty<>(Cursor.DEFAULT);
  38. final ObjectProperty<Cursor> CURSOR_WAIT = new SimpleObjectProperty<>(Cursor.WAIT);
  39.  
  40. @Override public void start(final Stage primaryStage) {
  41. primaryStage.setTitle("Combo Sample");
  42. Label label1 = new Label();
  43. Label label2 = new Label();
  44.  
  45. ComboBox<String> from = new ComboBox<>();
  46. from.getItems().addAll("DEV", "TEST", "PROD");
  47. from.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
  48. pool.submit(task1);
  49. label1.textProperty().bind(task1.messageProperty());
  50. });
  51.  
  52. ComboBox<String> to = new ComboBox<>();
  53. to.getItems().addAll("DEV", "TEST", "PROD");
  54. to.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
  55. pool.submit(task2);
  56. label2.textProperty().bind(task2.messageProperty());
  57. });
  58.  
  59. HBox layout = new HBox(10);
  60. layout.getChildren().addAll(new VBox(from, label1), new VBox(to, label2));
  61. primaryStage.setScene(new Scene(layout));
  62. primaryStage.setWidth(400);
  63. primaryStage.setHeight(200);
  64. primaryStage.show();
  65. primaryStage.getScene().cursorProperty().bind(Bindings.when(Bindings.or(task1.runningProperty(), task2.runningProperty()))
  66. .then(CURSOR_WAIT).otherwise(CURSOR_DEFAULT));
  67.  
  68. primaryStage.setOnCloseRequest(event -> {
  69. pool.shutdownNow();
  70. });
  71. }
  72.  
  73. Task<Void> task1 = new Task<Void>() {
  74. @Override protected Void call() throws Exception {
  75. System.out.println("selectFrom - Start");
  76. for(int i=1; i<=20; i++) {
  77. updateMessage(String.valueOf(i));
  78. Thread.sleep(1000);
  79. }
  80. System.out.println("selectFrom - End");
  81. return null;
  82. }
  83. };
  84.  
  85. Task<Void> task2 = new Task<Void>() {
  86. @Override
  87. protected Void call() throws Exception {
  88. System.out.println("selectTo - Start");
  89. for(int i=1; i<=5; i++) {
  90. updateMessage(String.valueOf(i));
  91. Thread.sleep(1000);
  92. }
  93. System.out.println("selectTo - End");
  94. return null;
  95. }
  96. };
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement