Search code examples
javacontrollerfxml

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>


Solution

  • By doing @FXML private Label animalName = new Label(); you construct a new label instead of the one already created by the fxml. In fact you do it twice : this Label is initialized again in openDiagnosticWindow by animalName = new Label();
    Just use @FXML public Label animalName;

    Another problem is in those lines:

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

    dc is a reference to an instance of DiagnosticController but not the instance used by the fxml.

    To get a reference of the controller used by the fxml you need to get it from the loader:

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

    which means loading Diagnostic.fxml should be done by PetshopController and not by DiagnosticController.

    For more help please post mcve. Important information is missing (like what is the name of the fxml file posted ? What invokes PetshopController ? and more) which forces us to guess.