1

I created a Maven JavaFX project using the archetype that I found here: https://github.com/openjfx/javafx-maven-archetypes

Everything runs smoothly, I can run a project with some labels, a text box and some buttons without issues. However, when I try adding in a WebView element (although recognized by the Scene Builder in IntelliJ) I cannot get the imports to work.

My pom.xml:

<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>org.example</groupId>
    <artifactId>untitled3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-web -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-web</artifactId>
            <version>16</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.3</version>
                <configuration>
                    <mainClass>org.dianavrabie.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

My main fxml document to describe the view:

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

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<VBox alignment="TOP_CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.dianavrabie.PrimaryController">
   <padding>
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
   </padding>
   <children>
      <AnchorPane prefHeight="200.0" prefWidth="200.0">
         <children>
            <Label layoutX="14.0" layoutY="14.0" text="POLYNOMIAL CALCULATOR" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
               <font>
                  <Font name="System Bold" size="36.0" />
               </font></Label>
            <Label layoutY="53.0" text="Polynomial 1" AnchorPane.leftAnchor="0.0">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <Label layoutY="90.0" text="Polynomial 2" AnchorPane.leftAnchor="0.0">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <TextField layoutX="124.0" layoutY="54.0" prefHeight="25.0" prefWidth="359.0" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="0.0" />
            <TextField layoutX="117.0" layoutY="91.0" prefHeight="25.0" prefWidth="359.0" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="0.0" />
            <Button layoutY="136.0" mnemonicParsing="false" text="ADD" AnchorPane.leftAnchor="0.0">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="57.0" layoutY="136.0" mnemonicParsing="false" text="SUBTRACT" AnchorPane.leftAnchor="57.0">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="156.0" layoutY="136.0" mnemonicParsing="false" text="MULTIPLY">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="251.0" layoutY="137.0" mnemonicParsing="false" text="DIVIDE">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="324.0" layoutY="137.0" mnemonicParsing="false" text="DERIVATIVE">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="432.0" layoutY="137.0" mnemonicParsing="false" text="INTEGRATE">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
            <Button layoutX="538.0" layoutY="137.0" mnemonicParsing="false" text="CLEAR" textFill="#fc5400">
               <font>
                  <Font size="14.0" />
               </font>
            </Button>
            
         </children>
      </AnchorPane>
   </children>
</VBox>

And the main app class

package org.dianavrabie;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

/**
 * JavaFX App
 */
public class App extends Application {

    private static Scene scene;


    @Override
    public void start(Stage stage) throws IOException {

        scene = new Scene(loadFXML("primary"), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }

    private static Parent loadFXML(String fxml) throws IOException {

        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

What do I need to do to get the WebView to work? I'm really new at this and I can't seem to find a solution.

Diana Vrabie
  • 45
  • 1
  • 8
  • Hello ! It may be of no use, but have you try to use the same JavaFx composant version ? You use Java version 11, Javafx version 13 for control and fxml and Javafx version 16 for javafx web... I would try to have an homogene version to avoid conflicts. – JN Gerbaux Oct 06 '21 at 19:29
  • I also have an issue adding a WebView, but I have an exception saying "module javafx.graphics does not export com.sun.javafx.sg.prism to unamed module" (and a bunch of errors of the same type). It can be solved by adding "--add-exports javafx.graphics/com.sun.prism=ALL-UNNAMED" (and a lot of other exports for each error) but it's strange to add all theses, I must be missing something. – JN Gerbaux Oct 06 '21 at 19:36

3 Answers3

1

I end up fixing theses WebView issues with the simple VM option to the command line tool launching my application:

--add-modules javafx.web

No need for others modules or exports as I was thinking, everything is ok with default javaFx module-info files.

enter image description here

JN Gerbaux
  • 151
  • 2
  • 6
0

If you want to use maven. You can. It is better to be coherent with the version of the 3 dependencies.As of today :

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

4.0.0

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

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>5.7.1</junit.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>17.0.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-web</artifactId>
        <version>17.0.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>17.0.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.6</version>
            <executions>
                <execution>
                    <!-- Default configuration for running with: mvn clean javafx:run -->
                    <id>default-cli</id>
                    <configuration>
                        <mainClass>com.example.demo1/com.example.demo1.HelloApplication</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Sylvain
  • 113
  • 7
0

A few questions:

  • version class version <> artifact's jar version compatibility?

  • module-info.java /*if presented */ requires javafx.web;

The absence of a one of these factors leads to the behavior of a project, when WebView is invisible for compiler

Simon
  • 130
  • 1
  • 5