Answer the question
In order to leave comments, you need to log in
How to troubleshoot issue in Spring Boot + JavaFX?
Good day!
I will try to describe the problem as briefly as possible.
I am using Spring Boot + JavaFX to create a small desktop application.
An example of JavaFX + Spring + Spring Security integration was taken from here -
https://github.com/emorgado/javafx-springboot-spri...
So, the problem itself:
1) By clicking on a menu item, a new window opens.
@FXML
public void addNewPatientMenu(ActionEvent event) {
try {
Stage newPatientStage = new Stage();
newPatientStage.setTitle("Добавить нового пациента");
PatientController patientController = applicationContext.getBean(PatientController.class);
Scene newPatientScene = new Scene(patientController.getView());
newPatientStage.setScene(newPatientScene);
newPatientStage.setMaximized(true);
newPatientStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
public void cancelPatientButtonAction(ActionEvent event) {
Stage stage = (Stage) cancelPatientButton.getScene().getWindow();
stage.close();
}
The exception is pretty self-explanatory: the anchor pane cannot be the root of two different scenes. Instead of creating a new scene every time, just replace the root of the existing scene:
public class FXMLController {
Logger log = LoggerFactory.getLogger(FXMLController.class);
private static String prefix = "Controller";
private Parent root;
public Parent load() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(getFxmlName()));
loader.setControllerFactory(clz -> this);
return loader.load();
}
private String getFxmlName(){
String name = this.getClass().getSimpleName();
name = name.replaceAll("\\.", "/");
if(name.endsWith(prefix)){
name = name.substring(0, name.lastIndexOf(prefix));
}
return String.format("/fxml/%s.fxml", name).toLowerCase();
}
public Parent getView() throws IOException{
if(root == null){
root = load();
}
return root;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question