Search code examples
javacontrollerfxml

problems connecting fields from .java files to fxml in the controller file when the fxml controller() is set OUTSIDE of the fxml


thanks in advance. This is my first go away from tutorials etc.. so bare with...

I have a main fxml screen that is the blank area for which projects (objects) will be created. The main fxml has a menu and I can create projects (via a dialogue window) that prompts a titledpane to be made.

The titledpane and its contents (project information) are a seperate fxml file.

When the titledpane is created, the contoller is set from the java file, Not the fxml. However when trying to link the fields to get data to the TitledPane Text boxes the conrtoller file doesn't link through and 'exmapleText' (see below) is 'never used' and greyed out.

@FXML
private Text exampleText; 

I have only ever set the controller in the FXML file before. Trying to cut the fxmlLoader.setController(this) and updating the fxml to include one means the titledpane crashes on fxml.load()

I have tried changing the <fx:root in the fxml file to just be a <TitledPane> give it it's own id, and add a field in the controller... but always crash at the same fxmlLaoader.load() in the controller class.

fxml file controller file

Many thanks, I hope it makes sense.


Solution

  • THere was an issue in the FXML file, with the Text block inside a text block. The correct code should have been two seperate text blocks arranged in a grid so the elements are next to each other.

    The correct code reads :

    <Text text="Name of project : " GridPane.columnIndex="0" GridPane.rowIndex="0" />        <Text fx:id="nameOfNewProject" GridPane.columnIndex="1" GridPane.rowIndex="0" />