yuchen han
yuchen han

Reputation: 29

How to set the default JavaFX version when creating a JavaFX application

When I was using Mac, I found that when creating a JavaFX project through IntelliJ, the default version used was 17.0. Due to the upgrade of MAC, I need to use the latest 23.0 version of JavaFX. Is there a way to change the default version without having to change the pom file every time to adapt to the Mac computer?

I need to modify the pom file every time I create a maven javafx project to continue building it. How can I modify it so that the pom file defaults to version 23.0 javafx

I know how to change pom.xml. But I want to create javafx23.0 version directly when IntelliJ creates a project. But IntelliJ can only change the SDK version when creating a javafx project, so I need help.

Upvotes: 1

Views: 81

Answers (2)

life888888
life888888

Reputation: 3129

ideaIC-2024.3.3 - idea-IC-243.24978.46

Editing Code Templates

Settings

File -> Settings...

001

File and Code Templates

File and Code Templates -> Other

JavaFX -> javafx-pom.xml

002

  • replace all <version>${context.getProperty("javafx.version")}</version> to <version>${javafx.version}</version>
  • change properties
  <properties>
    <javafx.version>23.0.2</javafx.version>
    <maven.compiler.source>23</maven.compiler.source>
    <maven.compiler.target>23</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    #if ($context.testRunnerId == "junit")<junit.version>${context.getVersion("org.junit.jupiter", "junit-jupiter-api")}</junit.version>#end
    #if ($context.hasLanguage("kotlin"))<kotlin.version>${context.getVersion("org.jetbrains.kotlin", "kotlin-maven-plugin")}</kotlin.version>#end
  </properties>

then click **OK**

New Project

003

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>temp4</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>temp4</name>

    <properties>
        <javafx.version>23.0.2</javafx.version>
        <maven.compiler.source>23</maven.compiler.source>
        <maven.compiler.target>23</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <junit.version>5.10.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
...

javafx-pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>${context.group}</groupId>
  <artifactId>${context.artifact}</artifactId>
  <version>${context.version}</version>
  <name>${context.moduleName}</name>

  <properties>
    <javafx.version>23.0.2</javafx.version>
    <maven.compiler.source>23</maven.compiler.source>
    <maven.compiler.target>23</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    #if ($context.testRunnerId == "junit")<junit.version>${context.getVersion("org.junit.jupiter", "junit-jupiter-api")}</junit.version>#end
    #if ($context.hasLanguage("kotlin"))<kotlin.version>${context.getVersion("org.jetbrains.kotlin", "kotlin-maven-plugin")}</kotlin.version>#end
  </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>
    #if ($context.hasLibrary("tilesfx"))<dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-web</artifactId>
      <version>${javafx.version}</version>
    </dependency>
      <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-swing</artifactId>
        <version>${javafx.version}</version>
      </dependency>
    #end
    #if ($context.hasLibrary("fxgl"))<dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-media</artifactId>
      <version>${javafx.version}</version>
    </dependency>#end
    #if ($context.hasLibrary("controlsfx"))<dependency>
      <groupId>org.controlsfx</groupId>
      <artifactId>controlsfx</artifactId>
      <version>${context.getVersion("org.controlsfx", "controlsfx")}</version>
    </dependency>#end
    #if ($context.hasLibrary("formsfx"))<dependency>
      <groupId>com.dlsc.formsfx</groupId>
      <artifactId>formsfx-core</artifactId>
      <version>${context.getVersion("com.dlsc.formsfx", "formsfx-core")}</version>
      <exclusions>
        <exclusion>
          <groupId>org.openjfx</groupId>
          <artifactId>*</artifactId>
        </exclusion>
      </exclusions>
    </dependency>#end
    #if ($context.hasLibrary("validatorfx"))<dependency>
      <groupId>net.synedra</groupId>
      <artifactId>validatorfx</artifactId>
      <version>${context.getVersion("net.synedra", "validatorfx")}</version>
      <exclusions>
        <exclusion>
          <groupId>org.openjfx</groupId>
          <artifactId>*</artifactId>
        </exclusion>
      </exclusions>
    </dependency>#end
    #if ($context.hasLibrary("ikonli"))<dependency>
      <groupId>org.kordamp.ikonli</groupId>
      <artifactId>ikonli-javafx</artifactId>
      <version>${context.getVersion("org.kordamp.ikonli", "ikonli-javafx")}</version>
    </dependency>#end
    #if ($context.hasLibrary("bootstrapfx"))<dependency>
      <groupId>org.kordamp.bootstrapfx</groupId>
      <artifactId>bootstrapfx-core</artifactId>
      <version>${context.getVersion("org.kordamp.bootstrapfx", "bootstrapfx-core")}</version>
    </dependency>#end
    #if ($context.hasLibrary("tilesfx"))<dependency>
      <groupId>eu.hansolo</groupId>
      <artifactId>tilesfx</artifactId>
      <version>${context.getVersion("eu.hansolo", "tilesfx")}</version>
      <exclusions>
        <exclusion>
          <groupId>org.openjfx</groupId>
          <artifactId>*</artifactId>
        </exclusion>
      </exclusions>
    </dependency>#end
    #if ($context.hasLibrary("fxgl"))<dependency>
      <groupId>com.github.almasb</groupId>
      <artifactId>fxgl</artifactId>
      <version>#if ($context.sdkFeatureVersion >= 17)${context.getBomProperty("fxgl17.version")}#else${context.getBomProperty("fxgl11.version")}#end</version>
      <exclusions>
        <exclusion>
          <groupId>org.openjfx</groupId>
          <artifactId>*</artifactId>
        </exclusion>
      </exclusions>
    </dependency>#end

    #if ($context.testRunnerId == "junit")<dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>${context.asPlaceholder("junit.version")}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>${context.asPlaceholder("junit.version")}</version>
      <scope>test</scope>
    </dependency>#end
    #if ($context.testRunnerId == "testng")<dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>${context.getVersion("org.testng", "testng")}</version>
      <scope>test</scope>
    </dependency>#end
    #if ($context.hasLanguage("groovy"))<dependency>
      <groupId>org.apache.groovy</groupId>
      <artifactId>groovy</artifactId>
      <version>${context.getVersion("org.apache.groovy", "groovy")}</version>
    </dependency>#end
    #if ($context.hasLanguage("kotlin"))<dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-stdlib</artifactId>
      <version>${context.asPlaceholder("kotlin.version")}</version>
    </dependency>#end
  </dependencies>
...
...

Upvotes: 4

jewelsea
jewelsea

Reputation: 159546

The JavaFX version currently generated in projects by the Idea New JavaFX Project wizard will create the project using a default JavaFX version (based I think on a template that ships with a given Idea version). Upgrading the Idea version will usually set the new JavaFX Project Wizard to generate projects using a new (recent) JavaFX version.

With Idea 2024.3 there is no option to specify the JavaFX version within the Idea New JavaFX Project Wizard UI (you could log a feature request with Idea for that if you wanted).

However, selecting any needed JavaFX version after the project is generated is very easy. For a Maven generated project, see:

As of 2025-02, the current version of JavaFX (OpenJFX) is 23.0.2. The minimum JDK version is Java 21. The latest LTS version is JavaFX 21.0.6, requiring Java 17+.

Description JavaFX
version
Java (JDK)
version
Current version of JavaFX 23.0.2 21 or later
Latest LTS version of JavaFX 21.0.6 17 or later

In the dependency section you will set the versions for JavaFX here, for example to change from 19 to 23.0.2, change all of the references for <version>19</version> to 23.0.2. Similarly for any other org.openjfx artifact dependencies you may add to your project e.g. javafx-media or javafx-web if you use those. These changes need to be made only once for any project generated by the wizard.

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>19</version>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-fxml</artifactId>
    <version>19</version>
</dependency>

The generated project is modular and does not use the JavaFX SDK (it uses JavaFX artifacts fetched from Maven Central instead), so the JavaFX SDK installation will be irrelevant unless you delete the module-info.java from the generated project and manually modify the VM runtime options for execution to refer to an SDK version of JavaFX.

Upvotes: 2

Related Questions