打开新 window 时未加载 FXML 标签值

FXML Label value is not loaded when new window is opened

When I want to open a new window on a click event I need to load some a value in a label, but the value is not refreshed. I have tried load the new window an instantiate the label and set a value to be displayed, but nothing happens. Bellow is the fxml and the code:

public class PetshopController implements Initializable {
@FXML
    public ListView<String> ListaProgramari;
@FXML
    public void completeTheAppointment(MouseEvent e) {
        try {
            String animalName = ListaProgramari.getSelectionModel().getSelectedItem();
            DiagnosticController dc = new DiagnosticController();
            dc.openDiagnosticWindow(animalName);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}

public class DiagnosticController implements Initializable{

    @FXML
    private Label animalName = new Label();
    @FXML
    public TextField newDiagnostic;
    @FXML
    public Pane AnimalDetails;
    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void openDiagnosticWindow(String animalLabel) {
        try {
            animalName = new Label();
            animalName.setText(animalLabel);
            BorderPane root = (BorderPane) FXMLLoader.load(getClass().getResource("/controller/Diagnostic.fxml"));
            Scene scene = new Scene(root, 600, 400);
            scene.getStylesheets().add(getClass().getResource("/controller/application.css").toExternalForm());
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.show();           

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Bellow is the FXML. This contains all the items from the new window which is opened the problem is at the label: "animalName":

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane prefHeight="303.0" prefWidth="452.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.DiagnosticController">
   <top>
      <MenuBar prefHeight="0.0" prefWidth="480.0" BorderPane.alignment="CENTER" />
   </top>
   <center>
      <SplitPane dividerPositions="0.6445182724252492" orientation="VERTICAL" prefHeight="200.0" prefWidth="160.0" BorderPane.alignment="CENTER">
        <items>
          <AnchorPane fx:id="AnimalDetails" minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
               <children>
                  <TextField fx:id="newDiagnostic" layoutX="190.0" layoutY="131.0" />
                  <Label layoutX="48.0" layoutY="135.0" prefHeight="17.0" prefWidth="120.0" text="Adauga Diagnostic" />
                  <Label layoutX="42.0" layoutY="75.0" prefHeight="17.0" prefWidth="120.0" text="Numele Animalului:" />
                  <Label fx:id="animalName" layoutX="189.0" layoutY="75.0" prefHeight="17.0" prefWidth="145.0" text="empty" />
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="49.0" prefWidth="450.0">
               <children>
                  <Button fx:id="completeConsultation" layoutX="172.0" layoutY="46.0" mnemonicParsing="false" onMouseClicked="#completeConsultation" prefHeight="36.0" prefWidth="79.0" text="Complete" />
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
   </center>
</BorderPane>

通过执行 @FXML private Label animalName = new Label();,您构建了一个新标签,而不是 fxml 已经创建的标签。事实上你做了两次:这个标签在 openDiagnosticWindow 中被 animalName = new Label();
再次初始化 只需使用 @FXML public Label animalName;

另一个问题在这些行中:

 DiagnosticController dc = new DiagnosticController();
 dc.openDiagnosticWindow(animalName); 

dc 是对 DiagnosticController 实例的引用,但 不是 fxml 使用的实例。

要获取 fxml 使用的控制器的引用,您需要从加载程序中获取它:

FXMLLoader loader = new FXMLLoader();
BorderPane root =  loader.load(getClass().getResource("/controller/Diagnostic.fxml")                                                                                .openStream());
DiagnosticController dc = (DiagnosticController)loader.getController();

这意味着加载 Diagnostic.fxml 应该由 PetshopController 而不是 DiagnosticController

如需更多帮助,请postmcve。重要信息丢失(比如 fxml 文件的名称是什么 posted ?什么调用了 PetshopController ?等等)这迫使我们猜测。