Pointing to an FXML file in javafx

Asked

Viewed 144 times

0

I created a project from scratch, was without any folder and opened the window perfectly, but when I put inside 7 folders and then started not to load. Does anyone know how I would point? I’m using Scene Builder as well.

The tree is like this:

projeto
└───src
    └───br
        └───com
            └───mask
                └───tarefas
                    └───login
                        └───LoginSystem (arquivo que tem que apontar para o FXML)
                        |
                        └───layout
                            └───LoginFXML (ArquivoFXML que tem que ser apontado)

The class that points is this:

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

public class LoginSystem extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("LoginFXML.fxml"));
        
        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  • In which folder the file LoginFXML.fxml is?

  • This in the last folder,layout, already the System that calls it is a folder before, login

1 answer

0

Good, I resolved pointing the controller file inside the FXML file

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="br.com.mask.tarefas.login.layout.LoginFXMLController">

And soon after I created a URL inside the Loginsystem file (file that points to FXML) and I put it inside the Pane instead of creating it inside.

URL arquivoFXML = getClass().getResource("/br/com/mask/tarefas/login/layout/LoginFXML.fxml");
    
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(arquivoFXML);
  • I recommend you create a utility class that has a method in which you pass "LoginFXML.fxml" and he returns "/br/com/mask/tarefas/login/layout/LoginFXML.fxml", if not you will have to repeat this path every time you upload a FXML file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.