-1

I'm working on a school project, and one of the tasks is to create a nice UI. I'm learning to use JavaFX FXML and am using it by hand and by scene builder. The problem I ran into is that the builder recognizes the Controller class, but won't recognize the variable inside said class.

My codes right now are as such:

Controller.java:

package program;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
public class Controller implements Initializable{

@FXML
private TreeView<String> mainView;

@Override
public void initialize(URL arg0, ResourceBundle arg1){

    TreeItem<String> rootItem = new TreeItem<>("All and everything");
    String s = "test";

    TreeItem<String> testItem = new TreeItem<>("TEST");

    rootItem.getChildren().add(testItem);
    

    mainView.setRoot(rootItem);
}

public void selectItem(){

}
}

test.fxml:

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

<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="program.Controller">
   <children>
      <TreeView fx:id="mainView" layoutX="14.0" layoutY="14.0" prefHeight="378.0" prefWidth="579.0" />
   </children>
</AnchorPane>

program.java:

package program;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;



public class program extends Application{

    
    @Override
    public void start(Stage stage){

        Parent root = new Parent(){};


        try{
            root = FXMLLoader.load(this.getClass().getResource("/test.fxml"));
            System.out.print("UI file found!");
        
        }
        catch (IOException e){
            System.out.print("Error using the UI file "+e.getCause());
        }
        catch (NullPointerException e2){
            System.out.print("UI file not found!");
        }

        Scene mainScene = new Scene(root, 500, 500, Color.PURPLE);
        stage.setScene(mainScene);
        stage.setFullScreen(true);

        stage.show();

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

module-info.java:

module program {
    requires javafx.controls;
    requires com.google.gson;
    requires javafx.fxml;
    exports program;
}

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>program</groupId>
    <artifactId>project</artifactId>
    <version>1.0</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>18</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>18</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.4</version>
                <configuration>
                    <mainClass>program.App</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <!-- Default configuration for running -->
                        <!-- Usage: mvn clean javafx:run -->
                        <id>default-cli</id>
                    </execution>
                    <execution>
                        <!-- Configuration for manual attach debugging -->
                        <!-- Usage: mvn clean javafx:run@debug -->
                        <id>debug</id>
                        <configuration>
                            <options>
                                <option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000</option>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- Configuration for automatic IDE debugging -->
                        <id>ide-debug</id>
                        <configuration>
                            <options>
                                <option>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</option>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- Configuration for automatic IDE profiling -->
                        <id>ide-profile</id>
                        <configuration>
                            <options>
                <option>${profiler.jvmargs.arg1}</option>
                <option>${profiler.jvmargs.arg2}</option>
                <option>${profiler.jvmargs.arg3}</option>
                <option>${profiler.jvmargs.arg4}</option>
                <option>${profiler.jvmargs.arg5}</option>
                            </options>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I'm obscuring the actual package name for security reasons, but that should be irrelevant. Package program is a placeholder for it.

My FXML-file is under src/main/resources and the main application class reads it just fine. The fxml just wont interact with the controller. Please excuse if it's a duplicate, I spent time searching for a similar issue and couldn't find one. I have triple-checked the names and imports.

EDIT: I'm getting an IOException in the main program. I know the file reads fine. Error message:

java.lang.reflect.InaccessibleObjectException: Unable to make field private javafx.scene.control.TreeView program.Controller.mainView accessible: module program does not "opens program" to module javafx.fxml

Added the minimal reproducible example and module-info.java.

  • Change `exports program;` (in file `module-info.java`) to `opens program;` exactly as the exception message tells you. – Abra Apr 14 '22 at 11:02
  • I added the module-info.java. I have included everything needed in the pom.xml and have no issues regarding it whatsoever. I think the issue here might have to do something with file locations, which would be pretty absurd. – GarbageGoblin Apr 14 '22 at 11:02
  • .. ahh .. the requirements for modules are described in the Introduction to FXML .. – kleopatra Apr 14 '22 at 11:10
  • @kleopatra are you referring to: https://openjfx.io/javadoc/12/javafx.fxml/javafx/fxml/doc-files/introduction_to_fxml.html – Abra Apr 14 '22 at 11:17
  • @Abra yeah sure, why do you ask .. and spare the asker the most minimal research ;) BTW: the current version is fx18 .. – kleopatra Apr 14 '22 at 11:20
  • @kleopatra maybe it was too subtle but my intention was to get the OP to click on the link since you didn't provide one when you referred him to that document. By the way, you are referring to the last section of that document, under the heading _Deploying an Application as a Module_, correct? (Again a hint to the OP.) – Abra Apr 14 '22 at 11:23
  • @Abra hehe, not too subtle just the contrary .. my intention was to give the asker only a _hint_ where to search, doing the search should be his/her task ;) Note to all: the javafx tag wiki has references to all basic doc and other helpful resources – kleopatra Apr 14 '22 at 11:25
  • Hey all, found the issue. I had to add opens program to javafx.fxml to the module-info file. That fixed everything. Thanks for help! – GarbageGoblin Apr 14 '22 at 11:27

1 Answers1

0

I do not know which IDEA you work. This is for IntelliJ:

When you work with JavaFX (without Maven), you have to create a module-info file.

In the module file you have to write:

module JavaFX {

requires javafx.controls;
requires javafx.fxml;
requires javafx.base;
requires javafx.graphics;

opens <PackgeName from your FXML File> to javafx.fxml;
exports <PackageName from Controller and App>;

}

For maven you have to need a pom file and create a .java file as a Launcher.

public class Launcher {

public static void main(String[] args) {
    Application.launch(<AppName>.class, args);
}

}

  • I'm using maven and VS Code currently. The application runs just fine, problem being FXML not working like it should. I have made catches so it just gives me a blank window. – GarbageGoblin Apr 14 '22 at 11:06