JavaFX: Location is required

2

This is the main code, I'm having problems with FXMLLoader, since I tried several ways and the path is never found.

package com.quixada.ufc.fbd.main;

import java.util.ArrayList;

import com.quixada.ufc.fbd.viewFXController.OnChangeScreen;

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


public class MainJavaFX extends Application {

private static Stage stage;
private static Scene mainScene;
private static Scene detailsScene;
private static ArrayList<OnChangeScreen> listeners = new ArrayList<>();

@Override
public void start(Stage primaryStage) {
    try {
        stage = primaryStage;
        primaryStage.setTitle("Exemplo");
        AnchorPane fxmlMain = (AnchorPane)FXMLLoader.load(getClass().getResource("../com/quixada/ufc/fbd/view/ViewLogin.fxml"));
        mainScene = new Scene(fxmlMain,400,400);

        AnchorPane fxmlDetails = (AnchorPane)FXMLLoader.load(getClass().getResource("/src/com.quixada.ufc.fbd.view/ViewCadastro.fxml"));
        detailsScene = new Scene(fxmlDetails,400,400);

        primaryStage.setScene(mainScene);
        primaryStage.show();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void changeScreen(String scr, Object userData) {
    switch (scr) {
    case "main":
        stage.setScene(mainScene);
        notifyAllListeners("main", userData);
        break;
    case "details":
        stage.setScene(detailsScene);
        notifyAllListeners("details", userData);

    }
}

public static void changeScreen(String scr) {
    changeScreen(scr, null);
}


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

public static void addOnChangeScreenListener(OnChangeScreen newListener) {
    listeners.add(newListener);
}

private static void notifyAllListeners(String newScreen, Object userData) {
    for (OnChangeScreen l : listeners)
        l.onScreenChanged(newScreen, userData);
}

I am organizing my packages as follows:

Could someone give me some help?

    
asked by anonymous 10.11.2018 / 15:51

3 answers

0

It's just a clutter in the use of the getResource method, see examples:

Feature in the same package:

FXMLLoader.load(getClass().getResource("arquivo.fxml"));

Different package feature:

// Considerando br.com.empresa.origem e br.com.empresa.diretorio 
FXMLLoader.load(getClass().getResource("/br/com/empresa/diretorio/arquivo.fxml"));

You do not need to src nor ../ unless it is outside the src folder.

    
14.11.2018 / 16:34
1

Both are correct friends, however it turned out that it was just a missing library problem. I was using the jfoenix library and when I switched the machine I forgot to export the new jfoenix.jar path, because the program did not execute.

The code looks like this to anyone who is curious:

public void start(Stage primaryStage) {
    try {
        stage = primaryStage;

        primaryStage.setTitle("MyApp");
        AnchorPane fxmlLogin = (AnchorPane)FXMLLoader.load(getClass().getResource("/com/quixada/ufc/fbd/view/ViewLogin.fxml"));
        LoginScene = new Scene(fxmlLogin,1077,720);

        AnchorPane fxmlCadastro = (AnchorPane)FXMLLoader.load(getClass().getResource("/com/quixada/ufc/fbd/view/ViewCadastro.fxml"));
        CadastroScene = new Scene(fxmlCadastro,1077,720);

        AnchorPane fxmlMain = (AnchorPane)FXMLLoader.load(getClass().getResource("/com/quixada/ufc/fbd/view/ViewMainUsuario.fxml"));
        MainScene = new Scene(fxmlMain, 1077, 720);

        AnchorPane fxmlDetalhes = (AnchorPane)FXMLLoader.load(getClass().getResource("/com/quixada/ufc/fbd/view/DetalhesPontoTurisiticoView.fxml"));
        DetalhesScene = new Scene(fxmlDetalhes, 1077, 720);


        primaryStage.setScene(LoginScene);
        primaryStage.show();

    } catch(Exception e) {
        e.printStackTrace();
    }
}
    
15.11.2018 / 17:10
0
AnchorPane fxmlMain = (AnchorPane)FXMLLoader.load(getClass().getResource("/view/ViewLogin.fxml"));
    
13.11.2018 / 13:52