09.04.2018 Views

tornadofx-guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3. Components<br />

Components<br />

JavaFX uses a theatrical analogy to organize an Application with Stage and Scene<br />

components.<br />

TornadoFX builds on this by providing View , Controller , and Fragment components.<br />

While the Stage , and Scene are used by TornadoFX, the View , Controller , and<br />

Fragment introduces new concepts that streamline development. Many of these<br />

components are automatically maintained as singletons, and can communicate to each<br />

other through TornadoFX's simple dependency injections or direct instantiation.<br />

You also have the option to utilize FXML which will be discussed in Chapter 10. But first, lets<br />

extend App to create an entry point that launches a TornadoFX application.<br />

App and View Basics<br />

To create a TornadoFX application, you must have at least one class that extends App . An<br />

App is the entry point to the application and specifies the initial View . It does in fact extend<br />

JavaFX Application , but you do not necessarily need to specify a start() or main()<br />

method.<br />

Extend App to create your own implementation and specify the primary view as the first<br />

constructor argument.<br />

class MyApp: App(MyView::class)<br />

A View contains display logic as well as a layout of Nodes, similar to the JavaFX Stage . It<br />

is automatically managed as a singleton. When you declare a View you must specify a<br />

root property which can be any Parent type, and that will hold the View's content.<br />

In the same Kotlin file or in a new file, extend a class off of View . Override the abstract<br />

root property and assign it VBox or any other Node you choose.<br />

class MyView: View() {<br />

}<br />

override val root = VBox()<br />

10

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

Saved successfully!

Ooh no, something went wrong!