jSpace+JavaFX Setup Guide

Table of Contents:

Contact Benjamin Bogø if you found a problem or have suggestions for improvements/additions.

1 Information for Students that had Course 02121

1.1 Java with Built-in JavaFX

In case you took the course 02121 in E21 or E22, then you likely have a JDK with Built-in JavaFX that you can just use that JDK in your current jSpace project and do NOT build using Maven. If you have not had the course 02121 at that time, use the solution below.

However, note that if you both have JavaFX from the JDK and from Maven (the solution below), your IDE will likely complain that the javafx modules are available from more than one source.

2 Add JavaFX Dependencies

2.1 Pre-requirements

You need to have installed jSpace using maven, see Install jSpace, and have created a Maven project as described in Setup jSpace Project.

2.2 Change the pom.xml File

Fill out the following information (if not, you have to change all the red parts manually):

Input: Maven Group Id (eventually look in you pom.xml file for it).
Input: Maven Artifact Id (eventually look in you pom.xml file for it).
Input: The Java version you are using.
Input: The JavaFX version you WANT to use.
Input: Package name for the Main class. Use <groupId>.<artifactId> if you do not have a suggestion.
Input: Name of Main class (excluding package name). Use TestJSpaceFX if you do not have a suggestion.

Then paste the following into your pom.xml file (the old contents should be deleted):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId><groupId></groupId>
    <artifactId><artifactId></artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source><java-version></maven.compiler.source>
        <maven.compiler.target><java-version></maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version><javafx-version></version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version><javafx-version></version>
        </dependency>
          <dependency>
              <groupId>io.github.pspaces.jspace</groupId>
              <artifactId>common</artifactId>
              <version>[0.0,)</version>
          </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.6</version>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>dk.dtu.TestJSpaceFX</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3 [Eclipse] Test that jSpace and JavaFX Works

3.1 Create TestJSpaceFX Class

Right-click on the project (<artifactId>). In the dropdown, choose New > Class.

An options dialog will now be shown. Set the Name of the class to TestJSpaceFX. At the top, set Package name to dk.dtu. Click on the button.

Copy the code below and paste it into the newly created TestJSpaceFX.java file (the old contents of the file should be deleted). Save the file.

package dk.dtu;

import org.jspace.FormalField;
import org.jspace.SequentialSpace;
import org.jspace.Space;

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.control.Button;
import javafx.scene.input.*;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TestJSpaceFX extends Application {

    public static void main(String[] args) throws InterruptedException {
        Space inbox = new SequentialSpace();
        inbox.put("Hello World!");
        Object[] tuple = inbox.get(new FormalField(String.class));
        System.out.println(tuple[0]);
        launch(args);
    }

    private int counter = 0;
    private Button button = new Button();

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        this.button.setText("Im a counter! Click ME!!!");
        this.button.setOnAction(this::handleClick);
        StackPane root = new StackPane();
        root.getChildren().add(this.button);
        Scene scene = new Scene(root, 300, 250);
        scene.addEventFilter(KeyEvent.KEY_PRESSED, this::handleKey);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void handleClick(ActionEvent event) {
        this.counter++;
        this.button.setText("" + this.counter);
    }

    private void handleKey(KeyEvent event) {
        if (event.getCode() == KeyCode.UP) {
            this.counter++;
        } else if (event.getCode() == KeyCode.DOWN) {
            this.counter--;
        } else {
            return;
        }
        this.button.setText("" + this.counter);
    }
}

3.2 Run Program

In the top menu bar, choose Run > Run Configurations.... In the left side, click on Maven Build. Above the type filer text input on the left side, there are some small icons. Click on the first (on hover, it should say New launch configuration). On the Main tab under Base directory:, click on . In the small dialog, select the correct project (NOT any of its folders!) and click on . In the Goals: input, type in clean javafx:run. Finally, click on and then click on the button.

If it works, you should be able to BOTH see Hello World in the terminal and a small dialog with a button. If that is the case jSpace and JavaFX works (you are done here)! Otherwise, make sure you entered all the correct things in Step 2.2.

4 [IntelliJ] Test that jSpace and JavaFX Works

4.1 Sync Maven

After editing the pom.xml file, there should be a small Maven refresh icon in the top right corner of the file editor. Click on it or nothing will work.

4.2 Create TestJavaFX Class

Right-click on the src folder in the project (<artifactId>). In the dropdown, choose New > Java Class and select dk.dtu.TestJSpaceFX as class name.

Copy the code below and paste it into the newly created TestJSpaceFX.java file (the old contents of the file should be deleted). Save the file.

package dk.dtu;

import org.jspace.FormalField;
import org.jspace.SequentialSpace;
import org.jspace.Space;

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.control.Button;
import javafx.scene.input.*;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TestJSpaceFX extends Application {

    public static void main(String[] args) throws InterruptedException {
        Space inbox = new SequentialSpace();
        inbox.put("Hello World!");
        Object[] tuple = inbox.get(new FormalField(String.class));
        System.out.println(tuple[0]);
        launch(args);
    }

    private int counter = 0;
    private Button button = new Button();

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        this.button.setText("Im a counter! Click ME!!!");
        this.button.setOnAction(this::handleClick);
        StackPane root = new StackPane();
        root.getChildren().add(this.button);
        Scene scene = new Scene(root, 300, 250);
        scene.addEventFilter(KeyEvent.KEY_PRESSED, this::handleKey);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void handleClick(ActionEvent event) {
        this.counter++;
        this.button.setText("" + this.counter);
    }

    private void handleKey(KeyEvent event) {
        if (event.getCode() == KeyCode.UP) {
            this.counter++;
        } else if (event.getCode() == KeyCode.DOWN) {
            this.counter--;
        } else {
            return;
        }
        this.button.setText("" + this.counter);
    }
}

4.3 Run Program

In the top-menu, choose View and in the dropdown choose Tool Windows > Maven. In the new window, click <artifactId> > Plugins > javafx > javafx:run to run the project. In case this is missing, please do Step 4.1.

If it works, you should be able to BOTH see Hello World in the terminal and a small dialog with a button. If that is the case jSpace and JavaFX works (you are done here)! Otherwise, make sure you entered all the correct things in Step 2.2.

5 [VSCode] Test that jSpace and JavaFX Works

5.1 Rebuild Project

In the left sidebar, then right-click the project (<artifactId>) under the JAVA PROJECTS and select Maven > Reload Project in the dropdown.

5.2 Create TestJavaFX Class

Again under JAVA PROJECTS in the project (<artifactId>), right-click on the src/main/java folder. In the dropdown, choose New Java Class and select dk.dtu.TestJSpaceFX as class name.

Copy the code below and paste it into the newly created TestJSpaceFX.java file (the old contents of the file should be deleted). Save the file.

package dk.dtu;

import org.jspace.FormalField;
import org.jspace.SequentialSpace;
import org.jspace.Space;

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.control.Button;
import javafx.scene.input.*;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TestJSpaceFX extends Application {

    public static void main(String[] args) throws InterruptedException {
        Space inbox = new SequentialSpace();
        inbox.put("Hello World!");
        Object[] tuple = inbox.get(new FormalField(String.class));
        System.out.println(tuple[0]);
        launch(args);
    }

    private int counter = 0;
    private Button button = new Button();

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        this.button.setText("Im a counter! Click ME!!!");
        this.button.setOnAction(this::handleClick);
        StackPane root = new StackPane();
        root.getChildren().add(this.button);
        Scene scene = new Scene(root, 300, 250);
        scene.addEventFilter(KeyEvent.KEY_PRESSED, this::handleKey);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void handleClick(ActionEvent event) {
        this.counter++;
        this.button.setText("" + this.counter);
    }

    private void handleKey(KeyEvent event) {
        if (event.getCode() == KeyCode.UP) {
            this.counter++;
        } else if (event.getCode() == KeyCode.DOWN) {
            this.counter--;
        } else {
            return;
        }
        this.button.setText("" + this.counter);
    }
}

5.3 Run Program

In the left sidebar under MAVEN, choose <artifactId> > Plugins > javafx > run and then click on the arrow next to run to run the project.

If it works, you should be able to BOTH see Hello World in the terminal and a small dialog with a button. If that is the case jSpace and JavaFX works (you are done here)! Otherwise, make sure you entered all the correct things in Step 2.2.