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(); } 
  • First, transfer the initialization of the root element to the createWindow method - Nikolay Belyakov

2 answers 2

In the method, a new window is created, but the fxml file is not loaded. You need to transfer the fxml download to the button click handler. And initialization Stage there too.

 public void food() { createWindow(); } @FXML private void createWindow() { 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(); } 
  • I edited the code, but the result is the same. The corrected code above is Dima
  • And the names of fxml files exactly match? I see addSpendAmount.fxml being loaded, and what is the /addSpendAmountController.fxml file ??? - Nikolay Belyakov
  • I was wrong in writing. addSpendAmount.fxml refers to addSpendAmountController.class. The names are all the same - Dima
  • ActionEvent is not needed there at all. Why is it generally passed to the createWindow method? Annotate the food @FXML method - Nikolay Belyakov
  • I added the code to the answer - Nikolay Belyakov

The problem was that the window was too small and the elements could not be seen))