Featured Post

Applying Email Validation to a JavaFX TextField Using Binding

This example uses the same controller as in a previous post but adds a use case to support email validation.  A Commons Validator object is ...

Wednesday, April 26, 2017

Kotlin RadioButtons and Enumerated Types in TornadoFX

A previous post showed how to create a JavaFX property based on an Enum linked to a set of RadioButtons.  Selecting a RadioButton changed the Enum property.  Changing the Enum property selected (and de-selected) a RadioButton.  This Kotlin code shows a recent enhancement in  TornadoFX 1.7.4 which wraps this functionality up using an extension function.

An example usage of this new function follows.

This code is from a file ToggleTest.kt.  An Enum is extended to include a display label which is different than the Enum value.

enum class ToggleTestEnum(val displayLabel : String) 
{ C1("Choice 1"), C2("Choice 2"), C3("Choice 3") }

class ToggleTestView : View() {

    val toggleTestProp = SimpleObjectProperty <ToggleTestEnum>>()

    override val root = vbox {
        togglegroup {
            ToggleTestEnum.values().forEach { 
              radiobutton(it.displayLabel, value = it) 
        button("Print Prop") {
            setOnAction {
                println("prop $toggleTestProp.value")


class ToggleTestApp : App(ToggleTestView::class)

The togglegroup() builder enumerates over the set of Enum values, creating a RadioButton.  The RadioButton constructor uses the value and the Enum displayLabel (a custom field).  the builder closes with binding statement linking the Enum-based property to the togglegroup control.

To demonstrate the program, there is a button which echoes the value to standard out.

No comments:

Post a Comment