0

I need to change text on action in my second Pane. For that I tried using:

try{
    HouseDA.setDefault(house);

    FXMLLoader loader = new FXMLLoader(); //Loads an object hierarchy from an XML document.
    loader.setLocation(MainApp.class.getResource("view/MainSideMenu.fxml")); // which document
    loader.load();

    MainSideMenuController controller = loader.<MainSideMenuController>getController();
    controller.refreshHouseInfo();

    }catch(Exception e){}

Code works perfectly fine, but text isn't changing. After debugging it looks like it creates another instance of my Pane and changes text in there. How can I access controller of my existing Pane so that I can change text?

Alyona
  • 1,682
  • 2
  • 21
  • 44
  • Identically to [this question](https://stackoverflow.com/questions/47156903/accessing-fx-controller-then-calling-method-which-updates-the-label-text-issue), you load the FXML (via `loader.load()`), which creates some UI elements, but the UI hierarchy you create by doing that is never displayed anywhere. Consequently when you call methods on the controller that change the UI, there is nothing to see (you are changing the text of something that is not displayed). – James_D Nov 07 '17 at 13:03
  • *"How can I access controller of my existing Pane?"* Get the controller when you load (and display) the pane from the FXML file. Then pass the controller to whoever needs access to it. Maybe https://stackoverflow.com/questions/29639881/javafx-how-to-use-a-method-in-a-controller-from-another-controller helps. – James_D Nov 07 '17 at 13:56

2 Answers2

0

I'm not sure if I undestrood your question, but you can try this.

  1. Create one FXML file with 2 panes.
  2. On the first pane create some element. (for example TextArea)
  3. On the second pane create for example some button.
  4. Give it to TextArea and the Button the ID. (fx:id="yourID") in FXML file.
  5. Create instances in Main controller class. (TextArea yourTextAreaID; Button yourButtonID)

Then you can change everything in myTextArea from others panes.

yourButtonID.setOnAction(event -> {
    yourTextAreaID.setText("something");
});

If a user will click on the button, then in the textArea, on the second pane, text will be set on "something". (If you want to show only one pane, you can managing it with setVisible(boolValue) method)

mySecondPane.setVisible(false);
Ben
  • 196
  • 15
0

I was searching same thing too, but couldn't find. But after converting Parent node to Scene, then we could get FxmlLoader from Scene. So it can get controller.

FXMLLoader fxmlLoader = (FXMLLoader) (this.baseBorderPane.getScene().getUserData());
IController controller = fxmlLoader.getController();
controller.something();

Hope it helps

Gin
  • 11
  • 2