JacpFX Quickstart

JacpFX projects have a typical maven project structure.

root
  |
  src
  | |
  | main
  |   |
  |   java
  |   |
  |   resources
  |    |
  |    bundles (Resource bundles)
  |    |
  |    fxml (FXML files)
  |    |
  |    styles (CSS files)
  |
  pom.xml


To start a new JacpFX project you may use a simple Java archetype or one of the JacpFX archetypes.

JacpFX maven quickstart

The JacpFX quickstart archetype provides a simple JacpFX project with one FXWorbench, two FXPerspective(s) (FXML and JavaFX), two FXComponent(s) (FXML and JavaFX) and two CallbackComponent(s).

Requirements

Since JacpFX 2, Java 8 and JavaFX 8 (u66) is prerequisite.

Create a project from simple quickstart archetype

mvn archetype:generate  -DarchetypeGroupId=org.jacpfx  -DarchetypeArtifactId=JacpFX-simple-quickstart  -DarchetypeVersion=2.1

To build the project go to project root and type:

mvn packge

After the compilation and packaging is finished you may go to the target folder and execute the jar:

cd target && java -jar project-name-app.jar


JacpFX from scratch

The goal of the following tutorial is to create a simple JacpFX application similar to the application created by the simple-quickstart archetype

Create a simple java project

To create a JacpFX application from scratch you may use a simple maven java project. To create one, type:

mvn archetype:create -DgroupId=your.simple.jacpfx.gid -DartifactId=your-simple-jacpfx-aid -DarchetypeArtifactId=maven-archetype-quickstart

Add folders and packages

Create a resources folder and following subfolders: bundles, fxml, styles in your-simple-jacpfx-aid/src/
When finished, go to src/main/java, create a quickstart folder and add workbench, perspective, component, fragment, config, main folders.

Add the JacpFX dependencies

Add following JacpFX dependencies:

The JacpFX API

<dependency>
    <groupId>org.jacpfx</groupId>
    <artifactId>jacpfx.API</artifactId>
    <version>${jacp.version}</version>
    <scope>compile</scope>
</dependency>

The JacpFX implementation

<dependency>
    <groupId>org.jacpfx</groupId>
    <artifactId>jacpfx.JavaFX</artifactId>
    <version>${jacp.version}</version>
    <scope>compile</scope>
</dependency>

The JacpFX controls

<dependency>
    <groupId>org.jacpfx</groupId>
    <artifactId>jacpfx.JavaFXControls</artifactId>
    <version>${jacp.version}</version>
    <scope>compile</scope>
</dependency>

The JacpFX launcher

The launcher project is responsible for the JacpFX-component instance creation. Currently a Spring- and a Simple- launcher is available, so all JacpFX-components are either simple Spring beans or simple POJOs. While the Spring launcher gives you access to the whole Spring universe, the Simple launcher provides a fast startup time.

<dependency>
    <groupId>org.jacpfx</groupId>
    <artifactId>jacpfx.JavaFXSpring</artifactId>
    <version>${jacp.version}</version>
    <scope>compile</scope>
</dependency>

or

<dependency>
    <groupId>org.jacpfx</groupId>
    <artifactId>jacpfx.JavaFXLauncher</artifactId>
    <version>${jacp.version}</version>
    <scope>compile</scope>
</dependency>

The Spring configuration

To start the Spring container we need to create either a spring.xml or a spring configuration class like this:

Simply put the BasicConfig class in the config packe created before. A best practice is to put all component and perspective ids as static members to this configuration class and to user this members to reference a specific id.

The ApplicationLauncher

The ApplicationLauncher contains the main method, the reference to the FXWorkbench implementation, the Spring configuration file and the package names to scann for JacpFX components. Create an ApplicationLauncher class in the main package.

Alternatively you can create a simple JacpFXLauncher without any Spring dependencies and capabilities:

The FXWorkbench

The FXWorkbench is the „root node“ of your JacpFX application. A FXWorkbench creates an application window, defines references to FXPerspective(s) and contains some basic configurations like the initial window size, toolbar definitions and menu definition. Create a JacpFXWorkbench in the workbench package.

The FXPerspective

Next we create a simple JavaFX based FXPerspective called PerspectiveOne in the perspective package. A perspective defines the basic layout structure of your view, contains references to FXComponent(s) and declares render targets where FXComponent(s) can render their view. PerspectiveOne creates a simple view with a SplitPane which contains two GridPanes, both of them registered as a FXComponent render target.(TARGETCONTAINERLEFT, TARGETCONTAINERMAIN). A FXComponent can now registers itself to be rendered (the view) in one of those areas.

The FXComponent(s)

Now we create two FXComponent(s), one with a JavaFX view and the other with a FXML view.

The JavaFX FXComponent

ComponentLeft registers itself to be rendered in TARGETCONTAINERLEFT defined in PerspectiveOne.

The FXML FXComponent

The next step is to create a FXML file in /resources/fxml/myview.fxml with the following content:

The FXML file
<GridPane  hgap="10" vgap="10" minHeight="-Infinity" minWidth="-Infinity"
          xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">

    <children>

        <TextArea fx:id="name" text=""/>
                <Button onAction="#sayHello" text="%hello" />
    </children>

</GridPane>


The corresponding FXController must not implement/return an JavaFX view, instead the root node of your FXML file will be passed as view.

The FXML FXController


Handling resource and fxml files

All resource files are located in src/main/resources. The language bundle files are located in src/main/resources/bundles, with the above example resourceBundleLocation = "bundles.languageBundle“ simply add a languageBundleX.properties file, while X is your default locale (en, de, fr, whatever). If you want to use english by default add a languageBundleen.properties file and set localeID = "en" in your @View,@Perspective… annotation.

Now we have created a minimal JacpFX application which you can execute by starting the ApplicationLauncher class in the main package. This small tutorial contains only basic examples to compose your application UI, for advanced topics like messaging feel free to read the documentation

shadow