public class MainWindowController { public void food(ActionEvent event) { Parent root = null; Stage stage = new Stage(); try { root = FXMLLoader.load(getClass().getResource("/fxml/addSpendAmount.fxml")); } catch (IOException e) { e.printStackTrace(); } createWindow(root, stage); stage.initOwner(((Node) event.getSource()).getScene().getWindow()); stage.show(); } private void createWindow(Parent root, Stage stage) { stage.setScene(new Scene(root, 300, 150)); stage.setTitle("Введите потраченую сумму"); stage.setResizable(false); stage.initModality(Modality.APPLICATION_MODAL); } /fxml/addSpendAmount.fxml
<Pane fx:controller="controllers.addSpendAmountController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <children> <TextField fx:id="indicateCost" layoutX="184.0" layoutY="188.0" /> <Button layoutX="384.0" layoutY="188.0" mnemonicParsing="false" onAction="#saveCost" text="Готово" /> </children> </Pane> controllers.addSpendAmountController.class
public class addSpendAmountController { public void saveCost(ActionEvent event) { } When you click the food button in the main window, the same-name method works in the MainWindowContrller class, a new window is created in this method, but there are no elements that I registered in the fxml file, i.e. an empty window is created. Why is this happening?
Edited code:
public void food(ActionEvent event) { createWindow(event); } private void createWindow(ActionEvent event) { Parent root = null; try { root = FXMLLoader.load(getClass().getResource("/fxml/addSpendAmount.fxml")); } catch (IOException e) { e.printStackTrace(); } Stage stage = new Stage(); stage.setScene(new Scene(root, 300, 150)); stage.setTitle("Введите потраченую сумму"); stage.setResizable(false); stage.initModality(Modality.APPLICATION_MODAL); stage.show(); }