JavaFX Setup Guide
Table of Contents:
- Part 1: Download JDK with JavaFX
- Part 2: [Eclipse] Add JDK
- Part 3: [Eclipse] Create JavaFX Project
- Part 4: [Eclipse] Check that JavaFX Works
- Part 5: [IntelliJ] Add JDK
- Part 6: [IntelliJ] Create JavaFX Project
- Part 7: [IntelliJ] Check that JavaFX Works
- Part 8: [VSCode] Create JavaFX Project
- Part 9: [VSCode] Check that JavaFX Works
- Part 10: [Terminal] Check that JavaFX Works
Contact Benjamin Bogø if you found a problem or have suggestions for improvements/additions.
1 Download JDK with JavaFX
1.1 (OPTIONAL) Java Abbreviations
Eclipse, IntelliJ, VSCode and I use different terms, namely JRE, JDK and SDK:
- JRE (Java Runtime Environment) is a Java distribution that just should be able to run compiled Java Applications.
- JDK (Java Development Kit) is a Java distribution to develop (run and compile) Java Applications.
- SDK (Software Development Kit) is a distribution to develop software (not only in Java).
JDK can be used in place of JRE. JDK can be used in place of SDK if the SDK should be used (only) for Java.
1.2 Download
Go to DTU Learn under course 02121. Choose Course Content > Content and find the section JavaFX Downloads. Download the version of Java with JavaFX to your OS.
1.3 (OPTIONAL) Verify File
Check the sha256 sum of the downloaded file and compare it with the one given on DTU Learn:
Windows
Use a Linux shell (Linux Subsystem for Windows or Git Bash), navigate to the folder with the downloaded file and run the following command:
sha256sum windows-x64-jdk-19+fx-19.zipLinux
Open a terminal, navigate to the folder with the downloaded file and run the following command:
sha256sum linux-x64-jdk-19+fx-19.tar.gzMacOS
Open a terminal, navigate to the folder with the downloaded file and run the following command:
shasum -a 256 macos-x64-jdk-19+fx-19.tar.gz1.4 Unpack Contents
Unpack the contents of the downloaded file (the location does not matter but it is recommended to place it where your other JDK's are, if you know where).
Windows
Use your default unpacking method.
Linux
Run the command (zfx = x extract contents of the given z compressed f file):
tar zfx linux-x64-jdk-19+fx-19.tar.gzMacOS
Use your default unpacking method (double click on the file).
Note/copy the location of the unpacked folder jdk-19+fx-19.
Input: Absolute path to the extracted jdk-19+fx-19 folder (place here to validate the path).
1.5 (ONLY MacOS) Allow Using the Downloaded JDK
If you are the lucky owner of a Mac, then you will very likely have to allow the downloaded JDK in order to use the executables in it because it comes from an untrusted developer i.e. downloaded from a site not approved by Apple. If you filled out the input field above, the commands below will likely trigger the warning for the three executables java, javac and jar (the onces you will need). Run them in a terminal (the current directory should not matter).
/bin/java -version
/bin/javac -version
/bin/jar -versionAfter getting the warning, the executable can be allowed in settings (might be outdated):
- Open
System Preferences. - Select the tab
Security & Privacy. - Select the tab
General(likely already selected). - The executable you just tried to run should be in the bottom with a button . Click on that button.
- In the Are you sure-dialog, click on .
- Run the command again - this time it should work.
2 [Eclipse] Add JDK
2.1 Open Settings
Open Eclipse. In the top menu bar, choose Window > Preferences (MacOS: Eclipse > Preferences). In the left side of the dialog, choose Java > Installed JREs.
2.2 Add JRE
Click on the button on the right. Select Standard VM and click on . For the JRE home field, click on the button and select the unpacked jdk-19+fx-19 folder (the path is ). Click on the button.
Note: If there is a small red cross in the top of the dialog, you selected the wrong folder.
2.3 Apply Changes
Warning: Do not change the selected check box in the list over JREs. If you do, your Eclipse might get stuck in recompiling all projects in the current workspace! In case it gets stuck, the solution is to reinstall Eclipse.
Click on the button.
3 [Eclipse] Create JavaFX Project
3.1 Create Project
In the top menu bar, choose File > New > Project. Select Java Project (default selected) and click on the button. Give your project a name.
Input: Name of the created project.
3.2 Select Correct JRE and Avoid Creating module-info.java
In the JRE section, choose Use a project specific JRE and make sure to select the added jdk-19+fx-19 JDK.
Next, in the Module box (if you have it - otherwise ignore this), uncheck the option Create module-info.java file.
Finally, click on the button.
If there comes a dialog saying Create module-info.java, then click on .
Selecting another JRE can be changed later by expanding your project (), right-click on JRE System Library [...] and select Properties in the dropdown.
3.3 (OPTIONAL) Configure Build Path
Only do this step if you have red lines and do not have the module-info.java file.
Expand your project () and right-click on JRE System Library [jdk-19+fx-19]. In the dropdown, select Build Path > Configure Build Path. In the left side of the dialog, choose Java Compiler. On this tab, remove the check mark from the Use '--release' option check box. Click on . Say to the next dialog about Compiler Settings Changed.
Note: This is needed if Eclipse does not know Java 19 as it for some reason does not want to run it correctly otherwise.
4 [Eclipse] Check that JavaFX Works
4.1 Create Project
It is assumed that you followed Part 3 such that you have an empty Eclipse project we can play with.
4.2 Create TestJavaFX Class
Right-click on the project (). In the dropdown, choose New > Class.
An options dialog will now be shown. Set the Name of the class to TestJavaFX (important!). At the top, delete the Package name (or enter it below). If you have the module-info.java file then you need to enter a Package name. Click on the button.
Input: Name of the package where TestJavaFX.java is located (leave empty if none).
Copy the code below and paste it into the newly created TestJavaFX.java file (the old contents of the file should be deleted). Save the file.
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 TestJavaFX extends Application {
public static void main(String[] args) {
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 (OPTIONAL) Changes when Using module-info.java
Go to Step 4.4 if you do not have the module-info.java file.
If you created module-info.java, then go to that file and paste the following (the old content should be deleted):
module {
// Needed to make Eclipse stop complaining about JavaFX.
requires javafx.base;
requires javafx.control;
// To allow running the code.
exports ;
}If there are red lines in TestJavaFX.java, then make sure you did Step 3.2 when setting up the project. If it does not help, then delete module-info.java and go to Step 4.4 - now assuming that module-info.java was never created.
4.4 Run Program
In case there are red lines in TestJavaFX.java, make sure you did Step 3.3 when setting up the project.
Click on the (green) run button. If it works, you should see a small dialog with a button AND JavaFX works (you are done here). If it does not work, continue to the next step.
4.5 (OPTIONAL) Create Run Configuration
In the top menu bar, choose Run > Run Configurations.... 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, select the correct Project and Main class (use the and buttons). Change to the JRE tab and make sure that the first option Project JRE (jdk-19+fx-19) is selected. Click on and then click on the button. If it still does not work, please contact Benjamin.
Next Step
- (OPTIONAL) Install SceneBuilder
5 [IntelliJ] Add JDK
It is assumed that you already have a project open in IntelliJ. Otherwise, these steps are possible in a slightly different way when creating a project.
5.1 Open SDK Settings
In the top menu, select File > Project Structure. In the (large) dialog, select Platform Settings > SDKs on the left.
5.2 Add SDK
In the second/middle column (next to the column with Platform Settings > SDKs) at the top, there is a plus icon. Click on it and choose Add JDK... in the dropdown and select the unpacked jdk-19+fx-19 folder* (the path is ). In the last (right column), set the name to jdk-19+fx-19 (instead of 19 or 19 (...)) in the top.
* Note: If you cannot see the folder in the IntelliJ explorer, then try one of the following (until the first that works!):
- Follow the path to
jdk-19+fx-19(the path is) as long as possible in the IntelliJ explorer. Afterwards, close the explorer and open it again. - Close the IntelliJ explorer, go to the
Project Settings > Projecttab in theProject Structuredialog and click on . Redo this step from the start. - Restart IntelliJ.
- In the top menu, select
File > Invalidate Caches. Uncheck all boxes exceptClear file system cache and Local History. Click onInvalidate and Restart. - Reinstall IntelliJ.
- Switch to Eclipse... :D
5.3 Apply Changes
Finish by clicking to apply the changes.
6 [IntelliJ] Create JavaFX Project
6.1 Create Project
In the top menu, select File > New > Project.... In the next dialog, select Java (not JavaFX!) and make sure to select the Project SDK as the SDK you added in Part 5, namely jdk-19+fx-19. Click on . In the template dialog, just click on (no template). In the final dialog, select a project name and click on .
Input: Name of the created project.
Note: IntelliJ will likely need time to index the JDK/SDK.
6.2 (OPTIONAL) Select Correct Project SDK
In the top menu, select File > Project Structure. In the (large) dialog, select Project Settings > Project on the right. Make sure that the Project SDK is jdk-19+fx-19. Click on .
Note: IntelliJ will likely need time to index the JDK/SDK.
7 [IntelliJ] Check that JavaFX Works
7.1 Create Project
It is assumed that you followed Part 6 such that you have an empty IntelliJ project we can play with.
7.2 Create TestJavaFX Class
Right-click on the src folder in the project (). In the dropdown, choose New > Java Class and select TestJavaFX as class name. In case the file is placed in a package, fill out the field below.
Input: Name of the package where TestJavaFX.java is located (leave empty if none).
Copy the code below and paste it into the newly created TestJavaFX.java file (the old contents of the file should be deleted). Save the file.
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 TestJavaFX extends Application {
public static void main(String[] args) {
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);
}
}7.3 Run Program
Next to the public class TestJavaFX and public static void main, click on one of the (green) run buttons and select Run 'TestJavaFX.main()' in the dropdown. If it works, you should see a small dialog with a button AND JavaFX works (you are done here). If it does not work, make sure that it is not indexing or building anything in the background (look in the bottom right corner). If it still does not work, continue to the next step.
7.4 (OPTIONAL) Create Run Configuration
Click on one of the (green) run buttons again and select Modify Run Configurations. In the dialog under Build and run, make sure that SDK of '' or jdk-19+fx-19 (they should be the same) is selected. Where it says TestJavaFX, click on the paper-icon and choose the class TestJavaFX. Click on and then run it as normal. If it still does not work, please contact Benjamin.
Next Step
- (OPTIONAL) Install SceneBuilder
8 [VSCode] Create JavaFX Project
It is assumed that you have installed Visual Studio Code (VSCode) and have installed the following extensions:
8.1 Create Project
Open the command palette (Windows/Linux: + + , MacOS: + + ) and find the option Java: Create Java Project....
Afterwards, it asks which project type that you should create. You should choose No build tools and NOT JavaFX!!!
Finally, you have to select where to create the project and give it a name.
Input: Name of the created project.
8.2 Select Correct Project JRE
In the left sidebar with the files under the EXPLORER tab, open the file settings.json under .vscode and paste the following content (if you have an advanced setup, the important part is to add the java.configuration.runtimes key):
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
],
"java.configuration.runtimes": [
{
"name": "JavaSE-19",
"path": "",
"default": true
}
]
}Save the file. Check that it says JRE System Library [JavaSE-19] under the JAVA PROJECTS in the left sidebar and that you see javafx.base in the list if you expand it.
9 [VSCode] Check that JavaFX Works
9.1 Create Project
It is assumed that you followed Part 8 such that you have an empty VSCode project we can play with.
9.2 Create TestJavaFX Class
Right-click on the src folder in the project (). In the dropdown, choose New File and enter TestJavaFX.java as the file name.
Copy the code below and paste it into the newly created TestJavaFX.java file (the old contents of the file should be deleted). Save the file.
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 TestJavaFX extends Application {
public static void main(String[] args) {
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);
}
}9.3 Run Program
Right-click on the file TestJavaFX.java (or in the code for the file). In the dropdown, choose Run Java near the bottom. If it works, you should see a small dialog with a button AND JavaFX works (you are done here). If it does not work, then right-click the project () under the JAVA PROJECTS in the left sidebar and select Rebuild Project in the dropdown.
Next Step
- (OPTIONAL) Install SceneBuilder
10 [Terminal] Check that JavaFX Works
Please enter the following information:
Input: Operating system (terminal).
Input: Absolute path to the bin folder in the extracted jdk-19+fx-19 folder WITH TRAILING SLASH (leave empty if in PATH-variable).
10.1 Create TestJavaFX.java File
Create a file named TestJavaFX.java with the following content:
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 TestJavaFX extends Application {
public static void main(String[] args) {
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);
}
}10.2 Open Terminal
Open a terminal in the folder where you created TestJavaFX.java.
Next Step
- (OPTIONAL) Install SceneBuilder