tornadofx根據不一樣的選擇顯示隱藏控件

import javafx.collections.FXCollections
import tornadofx.*

class LearnApp : App(ChooseUserView::class)

class User() {
    constructor(name: String, type: Int, isAdmin: Boolean): this() {
        this.name = name
        this.type = type
        this.isAdmin = isAdmin
    }

    val nameProperty = stringProperty()
    var name by nameProperty

    val typeProperty = intProperty()
    var type by typeProperty

    val isAdminProperty = booleanProperty()
    var isAdmin by isAdminProperty

    val passwordProperty = stringProperty()
    var password by passwordProperty
}

class UserModel(user: User? = null) : ItemViewModel<User>(user) {
    val name = bind(User::nameProperty)
    val type = bind(User::typeProperty)
    val isAdmin = bind(User::isAdminProperty)
    val password = bind(User::passwordProperty)
}

class ChooseUserView : View() {
    val ctrl: ChooseUserCtrl by inject()
    val selectedUser = UserModel(ctrl.users.first())

    override val root = form {
        fieldset("選擇用戶") {
            field("姓名") {
                combobox(selectedUser.itemProperty, ctrl.users) {
                    cellFormat {
                        text = it.name
                    }
                }
            }

            field("密碼") {
                visibleWhen(selectedUser.isAdmin)
                textfield(selectedUser.password)
            }
        }
    }
}

class ChooseUserCtrl : Controller() {
    val users = FXCollections.observableArrayList<User>()

    init {
        users.add(User("普通學生", 1, false))
        users.add(User("管理員", 2, true))
    }
}
相關文章
相關標籤/搜索