打开新窗口时未加载FXML标签值

时间:2019-01-04 08:00:22

标签: java controller fxml

  

当我想在click事件上打开新窗口时,我需要加载一些   标签中的值,但不会刷新该值。   我尝试加载新窗口,实例化标签并设置要显示的值,但没有任何反应。   波纹管是fxml和代码:

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();
        }
    }
  

下面是FXML。它包含新窗口中打开的所有项目,问题出在标签上:“ 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>

1 个答案:

答案 0 :(得分:0)

通过执行@FXML private Label animalName = new Label();,您将构造一个新标签,而不是fxml已经创建的标签。实际上,您执行了两次:openDiagnosticWindowanimalName = new Label();中再次初始化此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

要获取更多帮助,请发布mcve。缺少重要信息(例如发布的fxml文件的名称是什么?调用PetshopController的内容是什么?),这迫使我们猜测。