0

My code somehow fails to load the fxml file. I have checked the path to the file and it is correct. Am I missing something? I know that I can use SceneBuilder to design my GUI and add the functionality with separate controller files.

JAVA CODE:

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main4 extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        try {
            StackPane page = (StackPane) FXMLLoader.load(Main4.class.getResource("/home/emil/code/javaGUI/src/sample.fxml"));
            Scene scene = new Scene(page);
            primaryStage.setScene(scene);
            primaryStage.setTitle("FXML is Simple");
            primaryStage.show();
        } catch (Exception ex) {
            Logger.getLogger(Main4.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

FXML CODE:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<StackPane prefHeight="150.0" prefWidth="200.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <Button mnemonicParsing="false" text="Button" />
  </children>
</StackPane>
Emil Zahariev
  • 137
  • 3
  • 10

1 Answers1

1

You have use the absolute path from source code directory rather than full file system path.

For example if your package for the java source file is com.learnjavafx. Then file name should like /com/learnjavafx/sample.fxml.

But for your case, simply use sample.fxml because source code is not part of any specific package.