snti
snti

Reputation: 35

TornadoFX load multiple FXML files

I am working on a UI using Kotlin and TornadoFX where there is a "Main" Part, and depending on which button I press the "Main" Part of the View (currently an AnchorPane) should change like this:

Content of the main view when starting

Content after pressing a button

I tried loading in 2 FXML documents and adding the Content to my Main-Anchorpane like this:

override val root: BorderPane by fxml("/views/MainView.fxml")
val contentContainer: AnchorPane by fxid("contentContainer")

val contentBtn1: AnchorPane by fxml("/views/MainViewProject.fxml")

val btnProject: JFXButton by fxid("btnProject")

init {
    btnProject.setOnAction {
        contentContainer += contentBtn1
    }
}

but I get the following error:

javafx.fxml.LoadException: Controller value already specified.

Upvotes: 1

Views: 220

Answers (1)

snti
snti

Reputation: 35

I solved this by creating a new View for the second FXML file:

class MainProjectView : View("My View") {
   override val root: HBox by fxml()
}

Then injecting the View in my MainViewClass and adding it there:

    val mainProjectView: MainProjectView by inject()
    
    init {
        mainPane += mainProjectView
    }

Upvotes: 1

Related Questions