Javafx: Location is required

Asked

Viewed 658 times

2

This is the main code, I’m having problems regarding Fxmlloader, because I’ve 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: inserir a descrição da imagem aqui

Could someone give me some help?

  • Hello, Samuel. Post the code instead of images of it.

  • It’s really a problem in the path to your FXML, in case if fxml is in the same package you can simply put the file name.

3 answers

1

Both are correct friends, but turned out it was just a missing library problem. I was using the jfoenix library and when switching machines I forgot to export the new path of jfoenix.jar, because of that the program did not run.

The code is like this for anyone who’s 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();
    }
}

0

AnchorPane fxmlMain = (AnchorPane)FXMLLoader.load(getClass().getResource("/view/ViewLogin.fxml"));

0


It’s just a confusion in using the getResource method, see the examples:

Resource in the same package:

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

Feature in different package:

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

No need to put src nor ../ unless it is outside the src folder.

Browser other questions tagged

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