public void start(Stage stage) { stage.setTitle("ListView demo"); FlowPane rootNode = new FlowPane(10, 10); rootNode.setAlignment(Pos.CENTER); Scene scene = new Scene(rootNode, 200, 120); stage.setScene(scene); responseLabel = new Label("Select Transport Type"); ObservableList<String> transportTypes = FXCollections.observableArrayList("Train", "Car", "Airplane"); ListView<String> transportListView = new ListView(transportTypes); transportListView.setPrefSize(80, 80); MultipleSelectionModel<String> listViewSelectionModel = transportListView.getSelectionModel(); listViewSelectionModel .selectedItemProperty() .addListener( new ChangeListener<String>() { public void changed( ObservableValue<? extends String> changed, String oldValue, String newValue) { responseLabel.setText("Transport selected is " + newValue); } }); rootNode.getChildren().addAll(transportListView, responseLabel); stage.show(); }
@Override public void start(final Stage primaryStage) { // initialize the stage primaryStage.setTitle("Modal Confirm Example"); final WebView webView = new WebView(); webView.getEngine().load("http://docs.oracle.com/javafx/"); primaryStage.setScene(new Scene(webView)); primaryStage.show(); // initialize the confirmation dialog final Stage util = new Stage(StageStyle.TRANSPARENT); util.initModality(Modality.APPLICATION_MODAL); util.setScene( new Scene( StackPaneBuilder.create() .children( PaneBuilder.create().styleClass("modal-dialog-glass").build(), HBoxBuilder.create() .styleClass("modal-dialog-content") .children( LabelBuilder.create().text("Will you like this page?").build(), ButtonBuilder.create() .text("Yes") .defaultButton(true) .onAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { // take action and close the dialog. System.out.println( "Liked: " + webView.getEngine().getTitle()); primaryStage.getScene().getRoot().setEffect(null); util.close(); } }) .build(), ButtonBuilder.create() .text("No") .cancelButton(true) .onAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { // abort action and close the dialog. System.out.println( "Disliked: " + webView.getEngine().getTitle()); primaryStage.getScene().getRoot().setEffect(null); util.close(); } }) .build()) .build()) .build(), Color.TRANSPARENT)); util.getScene() .getStylesheets() .add(getClass().getResource("/assets/fxml/modal-dialog.css").toExternalForm()); // show the confirmation dialog each time a new page is loaded. webView .getEngine() .getLoadWorker() .stateProperty() .addListener( new ChangeListener<Worker.State>() { @Override public void changed( ObservableValue<? extends Worker.State> observableValue, Worker.State state, Worker.State newState) { if (newState.equals(Worker.State.SUCCEEDED)) { primaryStage.getScene().getRoot().setEffect(new BoxBlur()); util.show(); util.toFront(); } } }); }