Views
3 months ago

tornadofx-guide

7. Layouts and Menus

7. Layouts and Menus Layouts and Menus Complex UI's require many controls. It is likely these controls need to be grouped, positioned, and sized with set policies. Fortunately TornadoFX streamlines many layouts that come with JavaFX, as well as features its own proprietary Form layout. TornadoFX also has type-safe builders to create menus in a highly structured, declarative way. Menus can be especially cumbersome to build using conventional JavaFX code, and Kotlin really shines in this department. Builders for Layouts Layouts group controls and set policies about their sizing and positioning behavior. Technically, layouts themselves are controls so therefore you can nest layouts inside layouts. This is critical for building complex UI's, and TornadoFX makes maintenance of UI code easier by visibly showing the nested relationships. VBox A VBox stacks controls vertically in the order they are declared inside its block (Figure 7.1). vbox { button("Button 1").setOnAction { println("Button 1 Pressed") } button("Button 2").setOnAction { println("Button 2 Pressed") } } Figure 7.1 You can also call vboxConstraints() within a child's block to change the margin and vertical growing behaviors of the VBox . 88

7. Layouts and Menus vbox { button("Button 1") { vboxConstraints { marginBottom = 20.0 vGrow = Priority.ALWAYS } } button("Button 2") } You can use a shorthand extension property for vGrow without calling vboxConstraints() . vbox { button("Button 1") { vGrow = Priority.ALWAYS } button("Button 2") } HBox HBox behaves almost identically to VBox , but it stacks all controls horizontally left-to-right in the order declared in its block. hbox { button("Button 1").setOnAction { println("Button 1 Pressed") } button("Button 2").setOnAction { println("Button 2 Pressed") } } Figure 7.2 You can also call hboxconstraints() within the a child's block to change the margin and horizontal growing behaviors of the HBox . 89

Guide
Guide
GUIDE
Guide
Guide
GUIDE
Guide
GUIDE
Guide
GUIDE
GUIDE
GUIDE