Tornadofx: Passing arguments to view when switching to it

Created on 27 Aug 2018  路  3Comments  路  Source: edvin/tornadofx

Hello,

until now I am searching for a way to pass an argument to a view. I read all the guides I could find but none of them covered my problem.

Here is my code:

package parts.gui.views

class Setup : View() {

    override val root : VBox by fxml()
    //Do stuff

    init{
        //start Button
        start.setOnAction {
            //Open Connection
            val req = Requester()
            /*
            reg.send(Order(bla......))
             */
            //change view and pass socket
            replaceWith<Overview>() }

    }

}

As you can see, I create the view via fxml and configure it. But then I want to open a socket, send the inital information which I gathered with this view. Then I want to pass the socket to the next view for further interaction with the server.

Anything I have seen so far is about binding things together but I don麓t see how that will work for me in this case.

Most helpful comment

After running several rounds around the block, I found the solution. Pretty simple but it works as intended. I will post parts of it here, you can keep it as an example if you want.

class Setup : View() {

    override val root : VBox by fxml()

    /*Doing stuff */
    init{

        //Open Connection and place it in scope
        val req = RequesterModel(SimpleObjectProperty<Requester>(Requester("192.168.4.59")))
        val scope = Scope()
        setInScope(req, scope)
        val overview = find<Overview>(scope)
        //start Button
        start.setOnAction {
            replaceWith(overwiev)
        }

    }

  class RequesterModel(val req : SimpleObjectProperty<Requester>) : ViewModel()

class Overview : View(){
   override val root : VBox by fxml()

   private val req : RequesterModel by inject()

    //Doing other stuff
    init{
        //testing 
        println(req.req.value.address)
       }
}

I was pretty dumb and didn麓t understand your examples but now I feel confident that won麓t happen again in the future. Thank you for the short reminder.

All 3 comments

You can find more information about passing parameters and viewmodel/scopes (preferred way) here:

https://stackoverflow.com/questions/41198155/tornadofx-how-to-pass-parameter-to-fragment-on-every-instance

After running several rounds around the block, I found the solution. Pretty simple but it works as intended. I will post parts of it here, you can keep it as an example if you want.

class Setup : View() {

    override val root : VBox by fxml()

    /*Doing stuff */
    init{

        //Open Connection and place it in scope
        val req = RequesterModel(SimpleObjectProperty<Requester>(Requester("192.168.4.59")))
        val scope = Scope()
        setInScope(req, scope)
        val overview = find<Overview>(scope)
        //start Button
        start.setOnAction {
            replaceWith(overwiev)
        }

    }

  class RequesterModel(val req : SimpleObjectProperty<Requester>) : ViewModel()

class Overview : View(){
   override val root : VBox by fxml()

   private val req : RequesterModel by inject()

    //Doing other stuff
    init{
        //testing 
        println(req.req.value.address)
       }
}

I was pretty dumb and didn麓t understand your examples but now I feel confident that won麓t happen again in the future. Thank you for the short reminder.

Great :) I'll close the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MairwunNx picture MairwunNx  路  3Comments

orochies picture orochies  路  4Comments

Leo-Thura picture Leo-Thura  路  6Comments

leder11011 picture leder11011  路  5Comments

iwh718 picture iwh718  路  3Comments