Next: <ahref="#Installation-and-deployment"accesskey="n"rel="next">Installation and deployment</a>, Up: <ahref="dir.html#Top"accesskey="u"rel="up">(dir)</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="ClojureFX"></a>
<h1class="top">ClojureFX</h1>
<p>This is the documentation to ClojureFX, version 0.4.0.
</p>
<tableclass="menu"border="0"cellspacing="0">
<tr><tdalign="left"valign="top">•<ahref="#Installation-and-deployment"accesskey="1">Installation and deployment</a>:</td><td> </td><tdalign="left"valign="top">adding ClojureFX and probably tools.jar to your build tool.
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Getting-started"accesskey="2">Getting started</a>:</td><td> </td><tdalign="left"valign="top">the little ceremony necessary to get a window “up and running”.
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Coding-a-scenegraph"accesskey="3">Coding a scenegraph</a>:</td><td> </td><tdalign="left"valign="top">for everyone who wants to write an UI the old-school way.
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#FXML-and-controllers"accesskey="4">FXML and controllers</a>:</td><td> </td><tdalign="left"valign="top">loading FXML files and generating a controller.
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Event-handling"accesskey="5">Event handling</a>:</td><td> </td><tdalign="left"valign="top">a short chapter about handling events.
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Roadmap"accesskey="6">Roadmap</a>:</td><td> </td><tdalign="left"valign="top">what’s up next?.
Next: <ahref="#Getting-started"accesskey="n"rel="next">Getting started</a>, Previous: <ahref="#Top"accesskey="p"rel="prev">Top</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Installation-and-deployment-1"></a>
<h2class="chapter">1 Installation and deployment</h2>
<p>The first, straightforward part of this is to add the dependency to your <samp>project.clj</samp> or <samp>build.boot</samp>, which consists simply of adding <code>[clojurefx "0.4.0"]</code>.
</p>
<p>For the users of <em>OpenJDK</em> 7 and 8, <em>OpenJFX</em>, the opensource implementation of JavaFX, is not included yet (it will be starting with OpenJDK 9). Luckily, many Linux distributions ship a separate OpenJFX package by now, but for those that don’t, the OpenJDK wiki has an article <ahref="https://wiki.openjdk.java.net/display/OpenJFX/Building+OpenJFX">“Building OpenJFX”</a>. Alternatively, you can of course install the Oracle JDK manually.
</p>
<hr>
<aname="Getting-started"></a>
<divclass="header">
<p>
Next: <ahref="#Coding-a-scenegraph"accesskey="n"rel="next">Coding a scenegraph</a>, Previous: <ahref="#Installation-and-deployment"accesskey="p"rel="prev">Installation and deployment</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
<p>To get the JavaFX environment up and running, you can’t just initialize some classes and fire up a window, as is the case with Swing; you first have to initialise the environment. For this, you have two choices: either use a “nasty hack” Oracle themselves show, or go down the Java road and subclass ‘<code>javafx.application.Application</code>’.
</p>
<p>For the “nasty hack”, you have to add a <code>defonce</code><em>before</em> you import JavaFX classes (so, best suited for a <samp>core.clj</samp> ns). You can then manually create a <code>Stage</code> and add a <code>Scene</code> to it.
<dd><p>This macro runs the code given on the JavaFX thread and immediately returns. Prefixing the s-exp with an @ has the same effect as using <code>run-now</code>.
</p></dd></dl>
<hr>
<aname="Coding-a-scenegraph"></a>
<divclass="header">
<p>
Next: <ahref="#FXML-and-controllers"accesskey="n"rel="next">FXML and controllers</a>, Previous: <ahref="#Getting-started"accesskey="p"rel="prev">Getting started</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Coding-a-scenegraph-1"></a>
<h2class="chapter">3 Coding a scenegraph</h2>
<p><strong>This part of the library has not been tested for a long time; I will get to it eventually, but expect things to be somewhat broken.</strong>
Up: <ahref="#Coding-a-scenegraph"accesskey="u"rel="up">Coding a scenegraph</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
<p><acronym>FXML</acronym> is an <acronym>XML</acronym> format describing a JavaFX user interface. It also allows defining action handlers and, similar to HTML, inline scripting via script tags. You can find an introduction <ahref="https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html">on this site</a>.
</p>
<p>ClojureFX provides an idiomatic interface to load FXML files, and in this new version can even generate a controller class for you at runtime.
Next: <ahref="#Generating-controller-classes"accesskey="n"rel="next">Generating controller classes</a>, Up: <ahref="#FXML-and-controllers"accesskey="u"rel="up">FXML and controllers</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Loading-FXML-files-1"></a>
<h3class="section">4.1 Loading FXML files</h3>
<p>So you created an <acronym>FXML</acronym> file, probably with the SceneBuilder, and obviously now want to use it in your application. Doing so looks tedious in the JavaFX docs, but it is actually straightforward. All you need is some place to add the loaded Node - this could be the Scene object, or simply any JavaFX Parent element. The loader function returns a pure javafx.scene.Node-based object.
<p>When creating an <acronym>FXML</acronym> file, you have built-in features to bind properties and call functions in an associated controller class. Before actually writing any Clojure, let’s see how you can prepare your <acronym>FXML</acronym> file to get the most out of it.
</p>
<p>First, at your outermost element in the file, you have to tell it the class name of its <acronym>JVM</acronym> sibling it is going to call. For that, open it, and add the <samp>fx:controller</samp> attribute: <code>fx:controller="ch.lyrion.MyController"</code>. It is not very important how you name the class, as long as it has a package and doesn’t exist anywhere else.
</p>
<p>To bind any element to your new controller (in the form of a <code>Property</code>), you need the <samp>fx:id</samp> attribute. Let’s try it with that label: <code><Label fx:id="MyLabel" /></code>. That way, you’ll always have access to it as long as you have the controller instance with you. Note that the CamelCase will be automatically converted to kebab-case when using the designated accessors from ClojureFX!
</p>
<p>Next, you can define action handlers. Note that “<em>Special Handlers</em>” (<ahref="https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#collections_and_property_handlers">as defined here</a>) are not yet fully supported; I’m working on them! You simply provide the attribute, e.g. an <samp>onAction</samp> attribute, with the method name prefixed with a pound sign; note that the method name CamelCase will be automatically converted to kebab-case. E.g. <code><Button onAction="#buttonClicked" /></code> will call <code>(button-clicked controller-instance event)</code> in the namespace you provided (see below).
</p>
<p>Now, finally, it’s time to weld the parts together. But wait! Your <acronym>FXML</acronym> file doesn’t have any companion, no controller class, let alone the <code>ch.lyrion.MyController</code> we told it to look for!
No worries, we got you covered. <ahref="#load_002dfxml_002dwith_002dcontroller">load-fxml-with-controller</a> has your and your file’s back. It doesn’t just load the <acronym>FXML</acronym> and returns a <code>Node</code>, it also parses the source and generates your file’s companion on the fly. For that, it needs a couple more infos than <code>load-fxml</code> though: first, of course, the file path, but also the fully qualified clojure function in <code>String</code> form that will be called when the class gets initialized by JavaFX. Note that all action handlers defined above also have to be in the namespace of that function.
</p>
<hr>
<aname="FXML-scripting"></a>
<divclass="header">
<p>
Next: <ahref="#FXML-API"accesskey="n"rel="next">FXML API</a>, Previous: <ahref="#Generating-controller-classes"accesskey="p"rel="prev">Generating controller classes</a>, Up: <ahref="#FXML-and-controllers"accesskey="u"rel="up">FXML and controllers</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="FXML-scripting-1"></a>
<h3class="section">4.3 FXML scripting</h3>
<p>Unfortunately, FXML scripting is currently broken (outdated JSR-223 implementation). Stay tuned!
</p>
<hr>
<aname="FXML-API"></a>
<divclass="header">
<p>
Previous: <ahref="#FXML-scripting"accesskey="p"rel="prev">FXML scripting</a>, Up: <ahref="#FXML-and-controllers"accesskey="u"rel="up">FXML and controllers</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
<dd><p>With this command, ClojureFX loads an FXML file and returns it as a <ahref="https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html">javafx.scene.Node</a>. Note that the filename will be parsed by <code>clojure.io/resource</code> before loading.
<dd><p>Like <ahref="#load_002dfxml">load-fxml</a>, but also generates and loads an accompanying controller class using <ahref="#generate_002dcontroller">generate-controller</a>.
</p></dd></dl>
<hr>
<aname="Event-handling"></a>
<divclass="header">
<p>
Next: <ahref="#Roadmap"accesskey="n"rel="next">Roadmap</a>, Previous: <ahref="#FXML-and-controllers"accesskey="p"rel="prev">FXML and controllers</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Event-handling-1"></a>
<h2class="chapter">5 Event handling</h2>
<p><strong>Coming soon.</strong>
</p>
<hr>
<aname="Roadmap"></a>
<divclass="header">
<p>
Next: <ahref="#Index"accesskey="n"rel="next">Index</a>, Previous: <ahref="#Event-handling"accesskey="p"rel="prev">Event handling</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Roadmap-1"></a>
<h2class="chapter">6 Roadmap</h2>
<ul>
<li> Allow for non-ActionEvent binding.
</li><li> Testing and fixing Scenegraph coding API.
</li><li> Fixing JSR-223 implementation.
</li></ul>
<hr>
<aname="Index"></a>
<divclass="header">
<p>
Previous: <ahref="#Roadmap"accesskey="p"rel="prev">Roadmap</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Index-1"></a>
<h2class="unnumbered">Index</h2>
<table><tr><thvalign="top">Jump to: </th><td><aclass="summary-letter"href="#Index_fn_letter-C"><b>C</b></a>