09.04.2018 Views

tornadofx-guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6. Type Safe CSS<br />

You can apply styles directly to a control by calling its addClass() function. Provide the<br />

MyStyle.tackyButton style to two buttons (Figure 6.2).<br />

class MyView: View() {<br />

override val root = vbox {<br />

button("Press Me") {<br />

addClass(MyStyle.tackyButton)<br />

}<br />

button("Press Me Too") {<br />

addClass(MyStyle.tackyButton)<br />

}<br />

}<br />

}<br />

Figure 6.2<br />

Intellij IDEA can perform a quickfix to import member variables, allowing<br />

addClass(MyStyle.tackyButton) to be shortened to addClass(tackyButton) if you prefer.<br />

You can use removeClass() to remove the specified style as well.<br />

Targeting Styles to a Type<br />

One of the benefits of using pure Kotlin is you can tightly manipulate UI control behavior and<br />

conditions using Kotlin code. For example, you can apply the style to any Button by<br />

iterating through a control's children , filtering for only children that are Buttons, and<br />

applying the addClass() to them.<br />

class MyView: View() {<br />

override val root = vbox {<br />

button("Press Me")<br />

button("Press Me Too")<br />

}<br />

}<br />

children.asSequence()<br />

.filter { it is Button }<br />

.forEach { it.addClass(MyStyle.tackyButton) }<br />

75

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!