wizeOnes
wizeOnes

Reputation: 129

How to add a combobox to a tableview in Kotlin using TornadoFx

I am trying to add a combobox to a tableview using tornadofx; the closest I have gotten to something that I think should work is this: column<AvailableRooms, ComboBox<String>>("Pets Allowed", combobox<String>(values = listOf<String>("3","5"))).

I have read the documentation, but it seems like a lot of it is half written, and unreliable. I am new to JavaFx and am learning as I go along. I learned Kotlin because I figured using their version of JavaFx (TornadoFx) would be easier to understand.

Any guidance would be greatly appreciated.

img: enter image description here

Update

TableView img: enter image description here

Upvotes: 0

Views: 1123

Answers (1)

Edvin Syse
Edvin Syse

Reputation: 7297

The second type parameter to the column builder is not the UI element type, but rather the value type, so in your case it should be String. Here is a complete sample application showcasing the use of a ComboBox in a TableView:

class Person {
    val nameProperty = SimpleStringProperty()
    var name by nameProperty

    val favoriteFruitProperty = SimpleStringProperty()
    var favoriteFruit by favoriteFruitProperty
}

class MyView : View() {
    val fruits = listOf("Apple", "Banana", "Pear")

    override val root = tableview<Person> {
        isEditable = true

        column("Name", Person::nameProperty)
        column<Person, String?>("Favorite fruit", Person::favoriteFruitProperty).useComboBox(fruits.observable())

        // Populate with test data. Don't try this at home
        asyncItems {
            listOf(Person().apply { name = "John"; favoriteFruit = "Apple" }, Person().apply { name = "Jane" })
        }
    }
}

The guide does require you to know at least some JavaFX basics, so it is definitely incomplete if you have no prior JavaFX knowledge. It shouldn't be unreliable though, so please let me know if there are errors in it, or if you have suggestions for improving it.

Upvotes: 3

Related Questions