GUI - JavaFX

  • If you don't already have it, download and unzip the JDK, these instructions use OpenJDK 11 jdk.java.net/archive/

    • You can move the unzipped folder to your root drive or your OneDrive

    • See IntelliJ page for more information

New JavaFX Application Project in IntelliJ with Java 11

One time per computer

  • Download and unzip JavaFX 11 SDK gluonhq.com/products/javafx/

    • You can move the unzipped folder to your root drive or your OneDrive

  • From Welcome screen...

    • Configure -> Structure for New Projects

      • Global Libraries + Java

      • Browse to and then expand the javafx-sdk/lib folder

      • Select all the jar files and only the jar files, OK

      • Change the name from javafx-swt to javafx11

One time per project

  • Create New Project -> Java FX

  • File -> Project Structure -> Global Libraries -> right click javafx11 -> Copy to Project Libraries, check box, OK, OK

    • Modules -> Dependencies -> + -> Library -> javafx11 (from project) -> Add Selected -> OK

  • If necessary, right click src -> New -> module-info.java (probably not necessary on Windows and probably necessary on MacOS)

    • Add the following between the top module line and the bottom }

requires javafx.fxml;

requires javafx.controls;


opens sample;

This really should work. If not, restart your computer and try it again.

Basic Single File JavaFX Project in IntelliJ using Java 11 and Maven

  • File -> New -> Project -> Maven (Project SDK should be 11) -> Next

  • Give project a meaningful name, set location to ~\OneDrive - Florida Gulf Coast University\IDEA Projects\project-name

    • Optionally, set Artifact Coordinates -> GroupId to a reverse domain name, like io.github.username

  • Finish

  • Enable auto-import

  • Expand project, expand src folder, expand main folder

  • Right click java folder -> New JavaFXApplication -> File name: Main

  • File -> Project Structure -> Modules -> Sources -> Language level: 11

  • File -> Settings -> Build, Execution, Deployment -> Java Compiler -> Module -> 11

  • Modify start method in Main:

@Override

public void start(Stage primaryStage) {

primaryStage.setTitle("Hello World!");

Button btn = new Button();

btn.setText("Say 'Hello World'");

btn.setOnAction(new EventHandler<ActionEvent>() {


@Override

public void handle(ActionEvent event) {

System.out.println("Hello World!");

}

});


StackPane root = new StackPane();

root.getChildren().add(btn);

primaryStage.setScene(new Scene(root, 300, 250));

primaryStage.show();

}

  • Alt-Enter to import libraries

    • Never import anything from AWT

  • Right click Main -> Run 'Main.main()'

FXML JavaFX Project in IntelliJ using Java 11 and Maven

  • File -> New -> Project -> Maven (Project SDK should be 11) -> Next

    • Name project and double check Project location. Should be something like C:\Users\username\OneDrive - Florida Gulf Coast University\IDEA Projects\projectname

    • GroupId can be blank or a reverse domain name like io.github.username

    • ArtifactId should be project name

    • Version can be left unchanged (or delete SNAPSHOT)

  • Finish

  • Edit pom.xml

  • click M to load Maven changes

  • File -> Project Structure -> Modules -> Sources -> Language level: 11

  • File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler -> Module -> 11

  • Follow Common FXML Project Instructions

FXML JavaFX Project in IntelliJ using Java 11 and Gradle

  • File -> Create New Project -> Gradle -> Java (Project SDK: 11) -> Next

  • Name project and double check Project location. Should be something like C:\Users\username\OneDrive - Florida Gulf Coast University\IDEA Projects\projectname

    • GroupId can be blank or a reverse domain name like io.github.username

    • ArtifactId should be project name

    • Version can be left unchanged (or delete SNAPSHOT)

  • Click Finish

  • Edit build.gradle (info)

plugins {

id 'java'

id 'application'

id 'org.openjfx.javafxplugin' version '0.0.7'

}


sourceCompatibility = 11


javafx {

modules = [ 'javafx.controls' , 'javafx.fxml' ]

version = '11.0.2'

}


mainClassName = 'Main'


group 'io.github.username'

version '1.0'


repositories {

mavenCentral()

}


dependencies {

testCompile group: 'junit', name: 'junit', version: '4.12'

}

  • Update username in group

  • Click Elephant at top right of screen or Ctrl+Shift+O

  • Follow Common FXML Project Instructions

  • Run -> Edit Configurations -> Modify options -> Add VM options -> paste the text below replacing the PathToYourJavaFXSDK with the path to your JavaFX SDK. You can find this through Windows Explorer (the folder icon). (no C: and forward slashes on a Mac) image

--module-path "C:\PathToYourJavaFXSDK\lib" --add-modules javafx.controls,javafx.fxml,javafx.base,javafx.media,javafx.graphics,javafx.swing,javafx.web

Common FXML Project Instructions

  • Expand project, expand src folder, expand main folder

  • Right click java folder -> New JavaFXApplication -> File name: Main (if you are making a Gradle project and don't see JavaFXApplication it means there is something wrong with your build.gradle)

  • Right click java folder -> New -> File -> Name: Controller.java (or something descriptive)

  • Right click resources folder -> New -> File -> sample.fxml (or something descriptive)

  • Modify start method in Main:

@Override

public void start(Stage primaryStage) throws Exception {

Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));


Scene scene = new Scene(root, 300, 275);


primaryStage.setTitle("FXML Welcome");

primaryStage.setScene(scene);

primaryStage.show();

}

  • Alt-Enter to import libraries

    • Never import anything from AWT

  • Modify Controller.java

import javafx.fxml.FXML;

import javafx.scene.control.Label;


public class Controller {


@FXML

private Label lblOutput;


public void sayHello() {

lblOutput.setText("Hello FXML!");


}


}

  • Modify sample.fxml

<?xml version="1.0" encoding="UTF-8"?>


<?import javafx.scene.control.Button?>

<?import javafx.scene.control.Label?>

<?import javafx.scene.layout.Pane?>



<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"

prefHeight="275.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/11.0.1"

xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">

<children>

<Label fx:id="lblOutput" layoutX="91.0" layoutY="129.0" prefHeight="17.0" prefWidth="118.0"/>

<Button layoutX="117.0" layoutY="89.0" mnemonicParsing="false" onAction="#sayHello"

text="Say Hello"/>

</children>

</Pane>

  • Right click Main -> Run 'Main.main()'

Scene Builder

JFoenix

For more (cool) controls, use JFoenix.

  1. Download appropriate jar (like for Java 8) from https://github.com/jfoenixadmin/JFoenix

  2. Open Scene Builder, click on gear icon in Library box, click JAR/FXML Manager, select downloaded jar file

Textbook

  • 25 JavaFX GUI: Part 1

  • 26 JavaFX GUI: Part 2

Troubleshooting

  • If you get the error "finished with non-zero exit value 1" try a different JDK for the Project SDK in File -> Project Structure -> Project

  • If you get strange color coding try a different JDK for the Project SDK in File -> Project Structure -> Project

  • If you get the error "JavaFX runtime components are missing, and are required to run this application" you need to do Run -> Edit Configurations -> VM configurations -> paste the text below replacing the PathToYourJavaFXSDK with the path to your JavaFX SDK. (no C: and forward slashes on a Mac)

--module-path "C:\PathToYourJavaFXSDK\lib" --add-modules javafx.controls,javafx.fxml,javafx.base,javafx.media,javafx.graphics,javafx.swing,javafx.web

  • The error "Caused by: java.lang.RuntimeException: Exception in Application start method" is almost always a problem with the fxml.