JavaFX FXML Label不会更新其值

时间:2016-02-01 20:22:01

标签: java javafx

我试图从另一个控制器更新标签,但它不会直观地更新。

以下控制器控件动态加载外部控制器。

private ClientShowController clientShowController; 

public void initialize(){ 

    try { 
        pane_clientDetail.getChildren().add(FXMLLoader.load(getClass().getResource("../resources/Client_show.fxml")));
        FXMLLoader loader = new FXMLLoader(getClass().getResource("../resources/Client_show.fxml")); 
        loader.load(); 

        clientShowController = (ClientShowController) loader.getController(); 
        System.out.println(clientShowController);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} 

private void showPersonDetails(Person person) {
    if (person != null) { 
        //Update 
        clientShowController.setId(Integer.toString(person.getId()));
    }
} 

第二个控制器:

@FXML private Label lbl_id;    

public void setId(String strId){
    System.out.println(strId);
    lbl_id.setText(strId);
    System.out.println(lbl_id);
}

请注意:

  • System.out正确显示标签上的id;
  • 第二个控制器绑定到嵌入第一个控制器的FXML文件。如代码所示,第二个FXML文件/控制器是动态加载的:pane_clientDetail.getChildren().add()
  • 在第二个控制器中从initialize方法设置id时,id会按预期显示在标签中:

    public void initialize(){     this.setId(" 12&#34); }

编辑:

第一个(主要)FXML定义:

<Pane fx:id="pane_clientDetail"/>

此窗格声明为属性

@FXML private Pane pane_clientDetail;

第二个:

<GridPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controllers.ClientShowController">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="144.0" minWidth="10.0" prefWidth="102.0" />
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="195.0" minWidth="10.0" prefWidth="195.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <Label text="given name" GridPane.rowIndex="1">
         <GridPane.margin>
            <Insets left="10.0" />
         </GridPane.margin>
      </Label>
      <Label text="family name" GridPane.rowIndex="2">
         <GridPane.margin>
            <Insets left="10.0" />
         </GridPane.margin>
      </Label>
      <Label text="gender" GridPane.rowIndex="3">
         <GridPane.margin>
            <Insets left="10.0" />
         </GridPane.margin>
      </Label>
      <Label text="email" GridPane.rowIndex="4">
         <GridPane.margin>
            <Insets left="10.0" />
         </GridPane.margin>
      </Label>
      <Label text="phone" GridPane.rowIndex="5">
         <GridPane.margin>
            <Insets left="10.0" />
         </GridPane.margin>
      </Label>
      <Label text="id">
         <GridPane.margin>
            <Insets left="10.0" />
         </GridPane.margin>
      </Label>
      <Label text="address" GridPane.rowIndex="6">
         <GridPane.margin>
            <Insets left="10.0" />
         </GridPane.margin>
      </Label>
      <Label text="zip" GridPane.rowIndex="7">
         <GridPane.margin>
            <Insets left="10.0" />
         </GridPane.margin>
      </Label>
      <Label fx:id="lbl_id" text="-" GridPane.columnIndex="1">
         <GridPane.margin>
            <Insets />
         </GridPane.margin>
      </Label>
      <Label fx:id="lbl_given_name" text="-" GridPane.columnIndex="1" GridPane.rowIndex="1" />
      <Label fx:id="lbl_family_name" text="-" GridPane.columnIndex="1" GridPane.rowIndex="2" />
      <Label fx:id="lbl_gender" text="-" GridPane.columnIndex="1" GridPane.rowIndex="3" />
      <Label fx:id="lbl_email" text="-" GridPane.columnIndex="1" GridPane.rowIndex="4" />
      <Label fx:id="lbl_phone" text="-" GridPane.columnIndex="1" GridPane.rowIndex="5" />
      <Label fx:id="lbl_address" text="-" GridPane.columnIndex="1" GridPane.rowIndex="6" />
      <Label fx:id="lbl_zip" text="-" GridPane.columnIndex="1" GridPane.rowIndex="7" />
      <Label text="city" GridPane.rowIndex="8">
         <GridPane.margin>
            <Insets left="10.0" />
         </GridPane.margin>
      </Label>
      <Label fx:id="lbl_city" text="-" GridPane.columnIndex="1" GridPane.rowIndex="8" />
   </children>
   <VBox.margin>
      <Insets top="10.0" />
   </VBox.margin>
</GridPane>

1 个答案:

答案 0 :(得分:2)

您似乎有两个UI实例:

1. pane_clientDetail.getChildren().add(FXMLLoader.load(getClass().getResource("../resources/Client_show.fxml")));

2. FXMLLoader loader = new FXMLLoader(getClass().getResource("../resources/Client_show.fxml")); 

首先加载视图并将其附加到pane_clientDetail,这是正在显示的视图。然后你创建另一个并获得它的控制器。由于显而易见的原因,第二个控制器无法更新第一个视图。

解决方案:

FXMLLoader loader = new FXMLLoader(getClass().getResource("../resources/Client_show.fxml"));
Parent view = loader.load();
pane_clientDetail.getChildren().add(view);
// rest of the code