将标签绑定到线程的永久更改值

时间:2018-08-29 14:40:51

标签: kotlin tornadofx

我有一个非常基本的问题,但找不到答案。我已经通过Google搜索了存在类似问题的人,但没有发现任何有用的信息。 (例如https://github.com/edvin/tornadofx-guide/blob/master/part1/11.%20Editing%20Models%20and%20Validation.md

我有以下ViewModel

class MasterSizeModel(var size : Int) : ViewModel()
{
    val value = bind { size.toProperty() }
}

并将其注入另一个类中,在其中执行以下操作:

masterSize.size = order.masterStatSize

masterSize是我的模型。

现在在第三堂课中,我想将标签中的值绑定到标签上。

private val recvMaster : Label by fxid("recvMaster")
/*....*/
recvMaster.bind(masterSizeModel.value)

但是不幸的是,我的尝试完全失败了。我可以从ModelView中看到大小正在按需更新,但是更改未显示在值中,而是显示在标签中。

编辑: 我完全忘记了绑定到textProperty()的方法,但是我再也没有得到任何帮助了。

recvMaster.textProperty().bind(masterSizeModel.value/*?*/)

编辑2: 在请求之后,我添加了完整的代码部分:

class Setup : View() {

override val root : VBox by fxml()

/*Adding Buttons and Textfields*/
init {

//Binding all checkboxes to their text field
//Input validation....

//start Button
start.setOnAction {

    val masterSize  = 0

    val masterSizeModel  = MasterSizeModel((masterSize))


  //Open a socket (see code below)
    val req = Requester(ipAdress.text, masterSizeModel)
    val reqModel = RequesterModel(SimpleObjectProperty<Requester>(req))
    val scope = Scope()
    setInScope(reqModel, scope)
    setInScope(masterSizeModel, scope)

    req.sendOrder(SetupOrder(/*Sending stuff throw the network*/))

    val overview = find<Overview>(scope)

    replaceWith(overview)
}

}

}

class Overview : View() {

    override val root : VBox by fxml()

    private val req : RequesterModel by inject()


    private val masterSizeModel  : MasterSizeModel by inject()

    private val recvMaster : Label by fxid("recvMaster")

     /*Adding buttons and stuff */


    init{
            /*A loop that will request stats from the server i keept it very simple so i can resolv the view problem first */
        var run= true
        val thread = thread(start= true, name="StatRequester"){while(run){req.req.get().sendOrder(StatOrder(OrderType.STAT))}}

        //Change the label whenever the number of recived messages ist raised
        recvMaster.textProperty().set(masterSizeModel.size.toString())


    }

}

class Requester(val address: String = "localhost", var masterSize: MasterSizeModel ) : Sender() {

    override val socket: ZMQ.Socket = context.socket(ZMQ.REQ)
    override val port = "4993"

    init {
        socket.connect("$protocol$address:$port")
    }

    override fun sendOrder(order: Order) {
      //ZeroMQ requires special care....
        val message = packOrder(order)
        val wrapper = packOrder(RequestOrder(order.type, message))
        socket.send(wrapper,0)
        val orderAsString = socket.recvStr(0)
        handleOrder(orderAsString)
    }

    private fun handleOrder(orderString: String)
    {
        val orderDedec = unpackOrder<RequestOrder>(orderString)
        when(orderDedec.type)
        {
            OrderType.STAT ->{
                val order = unpackOrder<StatOrder>(orderDedec.order)
                sleep(5000) //sleep for debugging only
                println("$masterSize, ${order.masterStatSize}")


               //Here I receive a new value and want to update the label in my view
                masterSize.size = order.masterStatSize

            }
            OrderType.STOP ->{
                close()
            }
            else ->{}
        }

    }

    override fun close() {
        socket.close()
        context.term()
    }

}

0 个答案:

没有答案