下面列出了javafx.scene.Scene#getRoot ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void renderMeasuredProbabilities(int[] results) {
CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();
BarChart<String, Integer> barChart = new BarChart(xAxis, yAxis);
barChart.setData(getChartData(results));
barChart.setTitle("Measured probability distribution");
StackPane root = new StackPane();
root.getChildren().add(barChart);
if (myStage != null) {
Scene oldscene = myStage.getScene();
VBox box = (VBox)(oldscene.getRoot());
oldscene.setRoot(new StackPane());
box.getChildren().add(root);
Scene newScene = new Scene(box);
newScene.getStylesheets().add(Main.class.getResource("/styles.css").toExternalForm());
myStage.setScene(newScene);
} else {
Stage stage = new Stage();
stage.setScene(new Scene(root, 640, 480));
stage.show();
}
}
/**
* Add the Windows menu
*/
public static void addWindowsMenu(final Scene scene) {
Parent rootNode = scene.getRoot();
if (rootNode instanceof Pane) {
Pane rootPane = (Pane) rootNode;
MenuBar menuBar = new MenuBar();
menuBar.setUseSystemMenuBar(true);
menuBar.getMenus().add(new WindowsMenu());
rootPane.getChildren().add(menuBar);
}
}
public static <T extends Object> void doBlockingAsyncWork(Scene scene, Supplier<T> action, Consumer<T> success,
Consumer<Throwable> error) {
Parent originalRoot = scene.getRoot();
Runnable blockScreen = () -> originalRoot.setDisable(true);
Runnable unblockScreen = () -> originalRoot.setDisable(false);
Task<T> task = new Task<T>() {
@Override
protected T call() throws Exception {
blockScreen.run();
return action.get();
}
@Override
protected void succeeded() {
unblockScreen.run();
success.accept(getValue());
}
@Override
protected void failed() {
unblockScreen.run();
error.accept(getException());
}
};
Thread t = new Thread(task);
t.setDaemon(true);
t.start();
}
@Override
public void start(Stage stage) {
stage.setTitle("ComboBoxSample");
Scene scene = new Scene(new Group(), 500, 270);
final ComboBox emailComboBox = new ComboBox();
emailComboBox.getItems().addAll("[email protected]", "[email protected]", "[email protected]",
"[email protected]", "[email protected]");
emailComboBox.setPromptText("Email address");
emailComboBox.setEditable(true);
emailComboBox.setOnAction((Event ev) -> {
address = emailComboBox.getSelectionModel().getSelectedItem().toString();
});
final ComboBox priorityComboBox = new ComboBox();
priorityComboBox.getItems().addAll("Highest", "High", "Normal", "Low", "Lowest");
priorityComboBox.setValue("Normal");
priorityComboBox.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param) {
final ListCell<String> cell = new ListCell<String>() {
{
super.setPrefWidth(100);
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
if (item.contains("High")) {
setTextFill(Color.RED);
} else if (item.contains("Low")) {
setTextFill(Color.GREEN);
} else {
setTextFill(Color.BLACK);
}
} else {
setText(null);
}
}
};
return cell;
}
});
button.setOnAction((ActionEvent e) -> {
if (emailComboBox.getValue() != null && !emailComboBox.getValue().toString().isEmpty()) {
notification.setText("Your message was successfully sent" + " to " + address);
emailComboBox.setValue(null);
if (priorityComboBox.getValue() != null && !priorityComboBox.getValue().toString().isEmpty()) {
priorityComboBox.setValue(null);
}
subject.clear();
text.clear();
} else {
notification.setText("You have not selected a recipient!");
}
});
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setHgap(10);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("To: "), 0, 0);
grid.add(emailComboBox, 1, 0);
grid.add(new Label("Priority: "), 2, 0);
grid.add(priorityComboBox, 3, 0);
grid.add(new Label("Subject: "), 0, 1);
grid.add(subject, 1, 1, 3, 1);
grid.add(text, 0, 2, 4, 1);
grid.add(button, 0, 3);
grid.add(notification, 1, 3, 3, 1);
Group root = (Group) scene.getRoot();
root.getChildren().add(grid);
stage.setScene(scene);
stage.show();
}
@Override
public void start(Stage stage) {
stage.setTitle("TitledPane");
Scene scene = new Scene(new Group(), 450, 250);
TitledPane gridTitlePane = new TitledPane();
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("First Name: "), 0, 0);
grid.add(new TextField(), 1, 0);
grid.add(new Label("Last Name: "), 0, 1);
grid.add(new TextField(), 1, 1);
grid.add(new Label("Email: "), 0, 2);
grid.add(new TextField(), 1, 2);
grid.add(new Label("Attachment: "), 0, 3);
grid.add(label, 1, 3);
gridTitlePane.setText("Grid");
gridTitlePane.setContent(grid);
final Accordion accordion = new Accordion();
for (int i = 0; i < imageNames.length; i++) {
images[i] = new Image(getClass().getResourceAsStream(imageNames[i] + ".jpg"));
pics[i] = new ImageView(images[i]);
tps[i] = new TitledPane(imageNames[i], pics[i]);
}
accordion.getPanes().addAll(tps);
accordion.expandedPaneProperty()
.addListener((ObservableValue<? extends TitledPane> ov, TitledPane old_val, TitledPane new_val) -> {
if (new_val != null) {
label.setText(accordion.getExpandedPane().getText() + ".jpg");
}
});
HBox hbox = new HBox(10);
hbox.setPadding(new Insets(20, 0, 0, 20));
hbox.getChildren().setAll(gridTitlePane, accordion);
Group root = (Group) scene.getRoot();
root.getChildren().add(hbox);
stage.setScene(scene);
stage.show();
}