Views
6 months ago

tornadofx-guide

7. Layouts and Menus

7. Layouts and Menus ContextMenu Most controls in JavaFX have a contextMenu property where you can assign a ContextMenu instance. This is a Menu that pops up when the control is right-clicked. A ContextMenu has functions to add Menu and MenuItem instances to it just like a MenuBar . It can be helpful to add a ContextMenu to a TableView , for example, and provide commands to be done on a table record (Figure 7.16). There is a builder called contextmenu that will build a ContextMenu and assign it to the contextMenu property of the control. tableview(persons) { column("ID", Person::id) column("Name", Person::name) column("Birthday", Person::birthday) column("Age", Person::age) } contextmenu { item("Send Email").action { selectedItem?.apply { println("Sending Email to $name") } } item("Change Status").action { selectedItem?.apply { println("Changing Status for $name") } } } Figure 7.16 108

7. Layouts and Menus Note there are also RadioMenuItem and CheckMenuItem variants of MenuItem available. The menuitem builders take the action to perform when the menu is selected as the op block parameter. Unfortunately, this breaks with the other builders, where the op block operates on the element that the builder created. Therefore, the item builder was introduced as an alternative, where you operate on the item itself, so that you must call setOnAction to assign the action. The menuitem builder is not deprecated, as it solves the common case in a more concise way than the item builder. ListMenu TornadoFX comes with a list menu that behaves and looks more like a typical ul/li based HTML5 menu. 109

GUIDE
guide
Guides
Guide
GUIDE
Guide
Guide
GUIDE
GUIDE TO
Guide
Guide
GUIDE
GUIDE
Guide
GUIDE
GUIDE
GUIDE
Guide
guide
Guide
guide
GUIDE
Guide
GUIDE
GUIDE
Guide
GUIDE
Guides
Guide
GUIDE