Views
3 months ago

tornadofx-guide

6. Type Safe CSS class

6. Type Safe CSS class MyApp: App(MyView::class, Styles::class) class MyView : View("My View") { override val root = vbox { label("Enter your login") form { fieldset{ field("Username") { textfield() } field("Password") { passwordfield() } } } button("Go!") { addClass(Styles.redStyle) } } } Figure 6.9 The stylesheet is applied to the application by adding it as a constructor parameter to the App class. This is a vararg parameter, so you can send in a comma separated list of multiple stylesheets. If you want to load stylesheets dynamically based on some condition, you can call importStylesheet(Styles::class from anywhere. Any UIComponent opened after the call to importStylesheet will get the stylesheet applied. You can also load normal text based css stylesheets with this function: importStylesheet("/mystyles.css") Loading a text based css stylesheet If you find you are repeating yourself setting the same CSS properties to the same values, you might want to consider using mixins and reusing them wherever they are needed in a Stylesheet . 84

6. Type Safe CSS Modifier Selections TornadoFX also supports modifier selections by leveraging and() functions within a selection. The most common case this is handy is styling for "selected" and cursor "hover" contexts for a control. If you wanted to create a UI that will make any Button red when it is hovered over, and any selected Cell in data controls such as ListView red, you can define a Stylesheet like this (Figure 6.10). Stylesheet import javafx.scene.paint.Color import tornadofx.Stylesheet class Styles : Stylesheet() { } init { button { and(hover) { backgroundColor += Color.RED } } cell { and(selected) { backgroundColor += Color.RED } } } App and View import tornadofx.* class MyApp: App(MyView::class, Styles::class) class MyView : View("My View") { and } val listItems = listOf("Alpha","Beta","Gamma").observable() override val root = vbox { button("Hover over me") listview(listItems) } 85

guide
A guide
Guide
guide
Guide
GUIDE
GUIDE
Guide
Guide
A FALCON GUIDE® - Falcon Guides
visitor guide visitor guide visitor guide visitor guide visitor guide ...