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, September 21, 2016

A TornadoFX Kotlin Context Menu with Alert Confirmation

This post demonstrates how to specify an action handler for the TornadoFX alert() function.  This code supports a ContextMenu delete operation.  The delete operation requires a confirmation.   "Cancel" will cancel the delete operation, closing the ContextMenu.  "OK" will adjust the TableView and call the back-end logic through a TornadoFX Controller class.

The purpose of the post is to show the syntax of Kotlin and TornadoFX rather than provide working code.  For that type of tutorial, check out courses.bekwam.net/public_tutorials.


This screenshot shows the TableView with a Delete User ContextMenu MenuItem.

A ContextMenu Created in JavaFX
Selecting Delete User brings up the following dialog.

A Confirmation Dialog Created with the TornadoFX alert() Function

The ContextMenu-creating code is associated with a TableRow by setting the contextMenu property of the TableRow.   The ContextMenu contains a single Delete User MenuItem.  The Delete User MenuItem has a handler that will execute the business logic.

tblUsers.rowFactory = Callback{
    tbl ->
        val row = TableRow<User>()
        row.onMouseClicked = EventHandler{
            mouseEvent ->
            if( mouseEvent.clickCount == 2 )
                selectItem(row.getItem())
        }
        val deleteUser = MenuItem("Delete User")
        deleteUser.onAction = EventHandler {
            event ->
                val toDelete = tblUsers.selectedItem
                if( toDelete != null ) {
                    alert(
                            type = Alert.AlertType.CONFIRMATION,
                            header = "Delete User",
                            content = "Delete User " + toDelete.username.get(),
                            actionFn = {
                                btnType ->
                                if (btnType.buttonData == ButtonBar.ButtonData.OK_DONE) {
                                    controller.deleteUser(toDelete)
                                    tblUsers.items.remove(toDelete)
                                }
                            }
                    )
                }
        }
        row.contextMenu = ContextMenu( deleteUser )
        row

}

The deleteUser.onAction property is assigned an event handler that first retrieves the selected from from the TableView.  This is a nullable retrieval, but because I followed by this statement with a null check, I don't have to specify the !! operator later in the code.

Next, I use the TornadoFX alert() function to perform the operation if confirmed.  The alert() function creates and shows a JavaFX Alert dialog.  With type, header, and content, I specify the labels and messages of the dialog.  Notice the named parameters.

For the actionFN parameter, I specify a function literal.  The btnType parameter is used to determine whether or not the OK or the Cancel was pressed.

The last line of the Callback is simply "row".  There is no "return row" needed.

This was a quick presentation of some Kotlin and TornadoFX syntax.  I plan on putting together a step-by-step tutorial that includes this code for the courses.bekwam.net/public_tutorials web site.

No comments:

Post a Comment