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.

3. Components<br />

import <strong>tornadofx</strong>.*<br />

class MyView1: View() {<br />

override val root = vbox {<br />

button("Go to MyView2") {<br />

action {<br />

replaceWith(MyView2::class)<br />

}<br />

}<br />

}<br />

}<br />

class MyView2: View() {<br />

override val root = vbox {<br />

button("Go to MyView1") {<br />

action {<br />

replaceWith(MyView1::class)<br />

}<br />

}<br />

}<br />

}<br />

Figure 3.10<br />

You also have the option to specify a spiffy animation for the transition between the two<br />

Views, as shown below.<br />

replaceWith(MyView1::class, ViewTransition.Slide(0.3.seconds, Direction.LEFT)<br />

This works by replacing the root Node on a given View with another View 's root .<br />

There are two functions you can override on View to leverage when a View's root<br />

is connected to a parent ( onDock() ), and when it is disconnected ( onUndock() ). You can<br />

leverage these two events to connect and "clean up" whenever a View comes in or falls<br />

out. You will notice running the code below that whenever a View is swapped, it will undock<br />

that previous View and dock the new one. You can leverage these two events to manage<br />

initialization and disposal tasks.<br />

Node<br />

26

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

Saved successfully!

Ooh no, something went wrong!