My javafx application has to switch its FXML file after the selection of an item in a combo box The combo box however stops responding after switching but the other items added by the FXML continue to work. I have to either minimise and maximise the application to make it work or use the widgets added by the FXML to make it responsive.
comboBox.setOnAction(event -> {
calculatorType = comboBox.getValue();
switch (calculatorType) {
case "Basic":
System.out.println(root.getChildren());
root.getChildren().clear();
try {
stage.setMinWidth(430);
stage.setMinHeight(530);
stage.setMaxWidth(430);
stage.setMinHeight(530);
//root.getChildren().remove(comboBox);
root.getChildren().add(FXMLLoader.load(getClass().getResource("/Calculator/Resources" +
"/BasicCalculator.fxml")));
comboBox.setLayoutX(320);
comboBox.setLayoutY(0);
root.getChildren().add(comboBox);
} catch (IOException e) {
e.printStackTrace();
}
break;
case "Scientific":
root.getChildren().clear();
try {
stage.setMinHeight(530);
stage.setMinWidth(780);
stage.setMaxHeight(530);
stage.setMaxWidth(780);
root.getChildren().add(FXMLLoader.load(getClass().getResource("/Calculator/Resources/" +
"ScientificCalculator.fxml")));
comboBox.setLayoutX(675);
comboBox.setLayoutY(0);
root.getChildren().add(comboBox);
} catch (IOException e) {
e.printStackTrace();
}
break;
case "Converter":
root.getChildren().clear();
stage.setMinHeight(530);
stage.setMinWidth(430);
stage.setMaxHeight(530);
stage.setMaxWidth(430);
try {
root.getChildren().add(FXMLLoader.load(getClass().getResource("/Calculator/Resources/" +
"Converter.fxml")));
} catch (IOException e) {
e.printStackTrace();
}
comboBox.setLayoutX(320);
comboBox.setLayoutY(0);
root.getChildren().add(ComboPane);
break;
}
});
The combo box is in main.
The code for combo box:
ComboBox<String> comboBox = new ComboBox<>();
comboBox.getStylesheets().add("Calculator/Resources/Styles/ComboBox.css");
comboBox.getItems().add("Basic");
comboBox.getItems().add("Scientific");
comboBox.getItems().add("Converter");
comboBox.getSelectionModel().select(0);
comboBox.setLayoutX(320);
comboBox.setLayoutY(0);
Thank You.
I suggest you use a different design. Below are two possibilities.
Make each type of calculator a separate Scene and just switch Scene
s by calling method setScene. Note that you may probably need to add the ComboBox
to each Scene
.
Alternatively, make each calculator a separate Pane and add each of those Pane
s to a StackPane. Switch between the calculator Pane
s by making the selected calculator's Pane
visible and all the others invisible.