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.