edvin / tornadofx

Lightweight JavaFX Framework for Kotlin
Apache License 2.0
3.68k stars 272 forks source link

ObservableStyleClass #88

Closed edvin closed 8 years ago

edvin commented 8 years ago

Some times you want to swap a style class on a node depending on some other state. It would be convenient to ble able to bind a Property<String> to a Node and have the value of the property reflected in the styleClass list of that node.

When the property changes, the old value would be removed from the style class and the new value would be added.

edvin commented 8 years ago

Stylesheet:

.success { -fx-background-color: green }
.danger { -fx-background-color: red }
.warning { -fx-background-color: orange }

Demo app:

class StyleClassView : View() {
    override val root = HBox()

    init {
        importStylesheet(TestStylesheet::class)

        with(root) {
            padding = Insets(70.0)

            // The observable property
            val selectedClass = SimpleStringProperty()

            // A combo box that updates the observable property
            combobox(selectedClass, listOf("success", "danger", "warning").observable())

            // Bind the current value as a styleClass on the HBox
            bindClass(selectedClass)
        }
    }
}

No selection

Success selected

Danger selected

Warning selected

edvin commented 8 years ago

@t-boom I put this functionality in CSS.kt. We can merge https://github.com/edvin/tornadofx/issues/80 into this file when you're ready :)

edvin commented 8 years ago

Completed, will be in the TornadoFX 1.4.3 release.