Search code examples
kotlinjavafxtornadofx

show dialog from controller


I want to show a dialog inside my tornadofx application, but I don't want to create the dialog in the view. I've tried to create a dialog at the controller, but this doesn't seem to work.

This is an working example on how I can create a dialog inside my View

class MainScreenSelect : View("tool") {
override val root = vbox {
    dialog("dialog") {
    // Code how the dialog looks and how it behaves 
    }
  }
}

My problem is, that I don't want to create my dialog inside my View, I want to create the dialog inside my controller. I assign the Vbox of my View to a variable inside my controller and want to create the dialog inside my controller.

This will be my View then

class MainScreenSelect : View("tool") {

private val controller : Controller by inject()
override val root = vbox {
    controller.vbox = this
    controller.showDialog()
  }
}

The Vbox of the View is assigned to a variable inside the controller and the next line should create a dialog.

My controller will look like this

class ChatScreenController : Controller() {
var vbox : Vbox by singleassign()

fun showDialog(){
vbox.apply{
dialog{} // Here is the error, I can't call dialog at this point, but I can 
       // call it if I do vbox.apply inside the View
}
}

My problem is, why can't I create the dialog inside my controller? I can create any other elements inside vbox.apply, like another vbox, button... , but no dialog. Where is my error and how can I create a dialog from a controller instead of a view?

edit: I already tried to create a dialog with

Dialog<R>().apply{
//CODE
}

This creates a dialog, but it doesn't lock my mainscreen to force an input and I can't close this window by pressing X(To be honest I didn't really know what I did with the Dialog, but if this is the way to go I'll look into it, how to work with this Dialog)


Solution

  • SOLVED: My mistake was, that I thought 'dialog' belonged to the UI Element vbox, but dialog is related to the View. Changing the variable inside the controller to View instead of an UI Element made it possible to create a dialog for this View.

    This is the new Controller

    class ChatScreenController : Controller() {
    var view : View by singleassign()
    
    fun showDialog(){
    view.dialog{
    // Code for the Dialog
    }
    }
    

    The View needs to be changed to assign itself to the variable view inside the controller

    class MainScreenSelect : View("tool") {
    
    private val controller : Controller by inject()
    override val root = vbox {
        controller.view = this@MainScreenSelect
        controller.showDialog()
    }
    }