Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

Extension Functions

Edvin Syse edited this page Feb 3, 2016 · 6 revisions

WikiDocumentationExtension Functions

Extension Functions

As mentioned in Type Safe Builders, Kotlin already makes JavaFX code much easier to write and reason about. However, some parts of the JavaFX API could benefit from some extra love. This section describes some of the additions TornadoFX includes to make your code more consice.

TableView and ListView

// Take action when user selects a row
table.onUserSelect {
    editCustomer(it)
}

// Take action when selection changes
table.onSelectionChange {
    showCustomerDetails(it)
}

// Take action when user deletes row
table.onUserDelete {
    deleteCustomer(it)
}

// Add column with valueProvider
table.addColumn<Customer, String>("Name") { it.value.name }

// Get selected item
val selected = table.selectedItem

TableColumn and ListView cell

// Configure updateItem for column
column.cellFormat { customer: Customer ->
    text = customer.name
    graphic = ImageView(customer.avatar)
}

Panes (Nodes that has children)

// replace all children
pane.replaceChildren(newComponent1, newComponent2)

// Reload stylesheets (also valid for Scene)
pane.reloadStylesheets()

Stage

// Reload stylesheets on stage focus
stage.reloadStylesheetsOnFocus()

All Nodes

// Manipulate styleclasses
val isHeading = node.hasClass("heading")
node.addClass("heading")
node.removeClass("heading")
node.toggleClass("heading", predicate)

// Add Node to Pane, alternatives
node.addTo(pane)
pane += node
pane.add(node)

GridPane

// Add row
pane.row {
    label("Name")
    textfield {
        promptText = "Customer name"
        addClass("big-input")
    }
}

EventTarget

// Determine if event happened inside a TableRow
val isInside = event.isInsideTableRow()

Other

// Convert a List to an ObservableList
val obs = list.observable()

Next: JsonModel

Clone this wiki locally