Home > Software design >  Using MaterialFX in IntelliJ
Using MaterialFX in IntelliJ

Time:04-21

I've been trying to use Maven Dependency

Also, I've added the requires org.glavo.materialfx.adapter; in module-info.java:

module org.example {
    requires javafx.controls;
    requires javafx.fxml;
    requires com.jfoenix;
    requires org.glavo.materialfx.adapter;

    opens org.example to javafx.fxml;
    exports org.example;
}

Does anyone have an idea how can I use this library because the author didn't really explain well how to do so. I would like to mention that it works perfectly in Scene Builder's latest version so I just wonder why it doesn't do so in IntelliJ.

CodePudding user response:

Use Maven (or Gradle) for dependency management

You are using Maven (at least that is what the screenshot shows in Idea, though it could be Gradle making use of a Maven repository).

You should define the dependency as a maven dependency in your pom.xml (or build.gradle) then reimport the build file into Idea.

You should not manually set library dependencies in Idea.

Idea and Maven will recognize that you have a modular project and, when you have the dependency defined in Maven, they will automatically put the new dependent module on the modulepath for compilation and execution.

The maven artifact can be found by searching the maven repository:

The dependency info is:

<dependency>
  <groupId>io.github.palexdev</groupId>
  <artifactId>materialfx</artifactId>
  <version>11.13.5</version>
</dependency>

The module-info for MaterialsFX requires VirtualizedFX. The VirtualizedFX module also needs to be on your module path. The pom.xml file for MaterialsFX has a includes a dependency on io.github.palexdev:virtualizedfx:11.2.6. So the dependent module will be accessible for your build and runtime automatically via Mavan and Idea's inbuilt integration with the Java Platform Module System.

Require the correct module name

The module name for the library is not org.glavo.materialfx.adapter, it is MaterialFX, so you should use:

requires MaterialsFX;

NOT:

requires org.glavo.materialfx.adapter;

I recommend that you spend some time studying tutorials for your build tool and the Java Platform Module System.

Example app

Example was created by running the idea new JavaFX project wizard, then modifying the resultant project.

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>com.example</groupId>
    <artifactId>material</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>material</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>18</version>
        </dependency>
        <dependency>
            <groupId>io.github.palexdev</groupId>
            <artifactId>materialfx</artifactId>
            <version>11.13.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.9.0</version>
                <configuration>
                    <source>18</source>
                    <target>18</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

module-info.java

module com.example.material {
    requires MaterialFX;   
    exports com.example.material;
}

MaterialApplication.java

package com.example.material;

import io.github.palexdev.materialfx.controls.MFXButton;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MaterialApplication extends Application {
    @Override
    public void start(Stage stage) {
        stage.setScene(new Scene(new MFXButton("mfx")));
        stage.show();
    }

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