0

I have a Pane component stored in a fxml file. I'm importing this component to another Scene, and I want this component to show with auto-generated information. So I searched this exact question and I found this: Passing parameters to a controller when loading an FXML. This what they do:

 private void addReservation(Reservation r) {
        try {
            FXMLLoader fxmloader = new FXMLLoader(getClass().getResource("/views/item_reserva.fxml"));
            Pane client = (Pane) fxmloader.load();
            Item_reservaController controller = fxmloader.<Item_reservaController>getController();
            controller.init(r);
            flowPane.getChildren().add(client);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

I'm loading the component to the Parent type in this case Pane. And then I'm calling its controller and initializing the components and finally showing the Pane inside a Flowpane.

This is what the init method does:

public void init(Reservation r) {
    this.reservation = r;
    labelCountry = new Label(r.getClients().getFirst().getCountry());
    labelRoom = new Label(Integer.toString(r.getRooms().size()));
    labelClient = new Label(Integer.toString(r.getClients().size()));
}

I guess that the problem is related with that the object I'm drawing is not the same as the controller I'm changing, so it wont change the information inside the real components.

4
  • I'm having trouble understanding what your problem is. Do you get an exception? Does the pane not show? Does it show without the Reservation info? Do you use the labels created in the init method? Commented Jul 12, 2016 at 19:51
  • It shows without the reservation info. And no the labels are already defined in the fxml file. Commented Jul 12, 2016 at 20:33
  • Where do you add the labels to the pane? Please include that part of the code as well. Commented Jul 12, 2016 at 21:25
  • The labels are defined in the fxml file. I used Scene Builder to create the FXML file, and I manually added the labels and use the ids shown Commented Jul 14, 2016 at 1:33

1 Answer 1

1

Since the init method is called after loading the fxml, it's unlikely that the Labels created in that method are added to the scene.

And no the labels are already defined in the fxml file.

Since the scene already contains the appropriate Labels, use those Labels instead of creating new ones. If the Labels are sucessfully injected to the controller, just use setText to assign the content:

public void init(Reservation r) {
    this.reservation = r;
    labelCountry.setText(r.getClients().getFirst().getCountry());
    labelRoom.setText(Integer.toString(r.getRooms().size()));
    labelClient.setText(Integer.toString(r.getClients().size()));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Have you overwritten the field value somewhere else? Were they sucessfully injected to the controller?
No I haven't, does this method works for you? Is there any other way to initialize a controller?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.