Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.application.Platform;
  3. import javafx.collections.FXCollections;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.ComboBox;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.layout.VBox;
  8. import javafx.stage.Stage;
  9.  
  10. public class TestEditableComboBox extends Application{
  11.  
  12. @Override
  13. public void start(Stage primaryStage) throws Exception{
  14.  
  15. ComboBox<String> comboBox = new ComboBox<String>(FXCollections.observableArrayList("item 1",
  16. "item 2",
  17. "editable"));
  18. comboBox.setMinWidth(100D);
  19. comboBox.setMaxWidth(100D);
  20. comboBox.valueProperty().addListener((observable,
  21. oldValue,
  22. newValue) -> {
  23.  
  24. if (newValue != null){
  25. if ("editable".equals(newValue)){
  26. // JavaFX 2.2: comboBox.setEditable(true);
  27. Platform.runLater(() -> comboBox.setEditable(true));
  28. }
  29. else{
  30. // JavaFX 2.2: comboBox.setEditable(true);
  31. Platform.runLater(() -> {
  32. comboBox.setEditable(false);
  33. comboBox.getSelectionModel().select(newValue);
  34. });
  35. }
  36. }
  37. });
  38.  
  39. VBox vBox = new VBox(new Label("Broken caret"),
  40. comboBox);
  41. Scene scene = new Scene(vBox);
  42. primaryStage.setScene(scene);
  43. primaryStage.show();
  44. }
  45.  
  46. public static void main(String[] args){
  47.  
  48. Application.launch(args);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement