0

I'm trying to update a label from another controller, yet it won't update visually.

The following controller controls dynamically loads the external controller.

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

The second controller:

@FXML private Label lbl_id;    

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

Note that:

  • The System.out is correctly displaying the id on the label;
  • The second controller is tied to an FXML file that is embedded in the first one. As the code shows, the second FXML file / controller is dynamically loaded: pane_clientDetail.getChildren().add().
  • When setting the id from the initialize method in the second controller, the id is displayed in the label as expected:

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

Edit:

The first (main) FXML defines:

<Pane fx:id="pane_clientDetail"/>

This pane is declared as property

@FXML private Pane pane_clientDetail;

The second:

<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
  • 1
    Can you add the definition of the fxml as well? What do you mean by embedded? Feb 1, 2016 at 20:23

1 Answer 1

2

It seems you have two instances of 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")); 

You first load a view and attach it to pane_clientDetail and this is the view that is being shown. Then you create another one and obtain controller to it. For obvious reasons 2nd controller won't update 1st view.

Solution:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.