0

I have a small question. I have a Menu.FXML which has a Controller (MenuController). Inside the Menu.FXML I Include another .FXMl (Inner.FXML) and it includes a Label. The Inner.FXML has a MouseClick event handler, so when I Click the Inner.FXML it does something, I want my Inner.FXML mouse listener to change the text which is inside the Menu.FXML. How can I do this? Thank you very much.

[CODE]

public class Main extends Application{

 @Override
 public void start(Stage stage) throws Exception {
     Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));

     Scene scene = new Scene(root);

     stage.setScene(scene);
     stage.show();
 }
}

InnerController:

public class InnerController implements Initializable {
    public void buttonClick(ActionEvent event){
        System.out.println("change label!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
   }    
}

Menu FXML:

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nestedcontroller.MenuController">
   <children>
      <Label text="Label" />
      <fx:include source="Inner.fxml" />
   </children>
</VBox>

InnerFXML.

<Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: green;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nestedcontroller.InnerController">
   <children>
      <Button layoutX="271.0" layoutY="187.0" mnemonicParsing="false" onAction="#buttonClick" text="Button" />
   </children>
</Pane>
3
  • Please post a MCVE Commented Feb 17, 2015 at 14:12
  • So what do you actually want the button to do? Change the text of a label? What are you changing it to? Commented Feb 17, 2015 at 14:37
  • I want the button from the InnerFXML to change the text of the Label which is inside the Menu.FXML to something different, like for now it's oke if it just changes to "monday". @James_D Commented Feb 17, 2015 at 14:42

1 Answer 1

1

Create and observable string property in the InnerContoller and set it from the button's handler. Then observe the property from the outer controller.

public class InnerController {

    private final ReadOnlyStringWrapper text = new ReadOnlyStringWrapper();

    public ReadOnlyStringProperty textProperty() { 
        return text.getReadOnlyProperty() ;
    }

    public String getText() {
        return text.get();
    }

    public void buttonClick(ActionEvent event){
        System.out.println("change label!");
        text.set("Hello world");
    }


    public void initialize() {
   }    
}

Then add an fx:id to your fx:include:

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nestedcontroller.MenuController">
   <children>
      <Label text="Label" fx:id="label" />
      <fx:include source="Inner.fxml" fx:id="innerPane" />
   </children>
</VBox>

And then in the MenuController just observe the property:

public class MenuController {
    @FXML
    private Label label ;
    @FXML
    private InnerController innerPaneController ;

    public void initialize() {
        innerPaneController.textProperty().addListener((obs, oldText, newText) -> 
            label.setText(newText));
        // or just label.textProperty().bind(innerPaneController.textProperty());
    }
}

See the FXML documentation for details.

Sign up to request clarification or add additional context in comments.

Comments

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.