How to set a controller for a fxml file that is in the resources folder of a maven project?

I am trying to develop my first javafx desktop app, but i foud myself in a deadlock. Firstly the program will read some excel files with sales, then create a database and summarise the total collected for 3 employees.But when i was creating a fxml file for the first scene, no matter what i did the compiler didn't locate the fxml file. After some research i found out that if i put the fxml inside a resouces folder it would work properly, but fater i had done that now i the fxml file doesn't recognize the Controller using the fx:controller option, probably because it isn't in the same directory anymore. i search some more and found a clue to how to fix it but could quite understand how to do it. in this question JavaFX and maven: NullPointerException: Location is required it says that it is possible to create a copy of the fxml files using the maven resources plugin Code:

<build>
...
<resources>
    <resource>
        <filtering>false</filtering>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.fxml</include>
        </includes>             
    </resource>
</resources>
...

But how can i do this? Where does the fxml files have to be the main directory folder or the resources folder? If i alter the data using scene builder of one does the other change as well? Do i need to add this maven resources plugin in the porn.xml?Here is some photos:

Main - Project

balanceus.fxml in the resource folder before trying fx:controller

running fine put without controller

then after add fx:controller

it breaks

And here is a link for the git hub with the files and with .txt of the complete error i get when tring to run:

github

Any Help is appreciated, also sorry for the bad english(not my first language).

Upvotes: 1

Views: 1923

Answers (1)

anko
anko

Reputation: 1708

I downloaded and analyzed your project and found some mistakes in the structure (and that an “open statement” in the module.info is missing) but after fixing them it still didn't work. :-(

So I created a new project from an archetype as described here: Getting Started with JavaFX > JavaFX and IntelliJ > Modular with Maven

When using „javafx-archetype-fxml“ you will get a basic structure which you need for your project. Then I copied some files of your project to the right places and it looks like this:

project structure

So as you can see the fxml files don't have to be in the main ressource folder. I put them in a separated folder named fxml.

Then I copied your App class and changed it to this:

package com.ultrasomma;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

import java.io.IOException;


public class App extends Application {

    @Override
    public void start(Stage stage) throws IOException {
        Scene scene = new Scene(FXMLLoader.load(App.class.getResource("fxml/balanceus.fxml")));
        scene.getStylesheets().add(App.class.getResource("css/BalanceUS.css").toExternalForm());
        stage.setTitle("BalanceUS");
        stage.getIcons().add(new Image(App.class.getResource("img/BalanceUS.png").toExternalForm()));
        stage.setResizable(false);
        stage.setScene(scene);
        stage.show();
    }

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

}

The module-info looks like this (you have to add the other stuff I abstracted from):

module com.ultrasomma {
    requires javafx.controls;
    requires javafx.fxml;

    // This statement is missing in your project:
    opens com.ultrasomma to javafx.fxml;

    exports com.ultrasomma;
}

If you do it like this, it should work for you too (no need to manipulate the pom file).


I am not sure if I understand this question of yours correctly:

If i alter the data using scene builder of one does the other change as well?

If you change "balanceus.fxml" then "balanceus2.fxml" will not change.


The reference for the controller in your fxml is fine btw.:

fx:controller="com.ultrasomma.Controller"

Upvotes: 2

Related Questions