Regemaster
Regemaster

Reputation: 11

How to load FXML file which is out of *.jar file

this is my first post on Stack, I was looking for answer on my question, but I haven't found anything useful. I'm learning Java, so be gentle ;)

My problem is, that I don't want to make One file with everything in "jar". I must load FXML file, which is out of *.jar file. For example: I'm making FXML file which is in my JavaFXMLApplication folder, I run "JavaFXApplication.jar" file and this Application use FXML which I just made.

I hope I describe it clearly :)

EDIT: I have found another solution, which works better for me...

    File file = new File("absolutPathToFile\\FXMLDocument.fxml");
    InputStream is = new BufferedInputStream(new FileInputStream(file));

    Pane root = new Pane();

    FXMLLoader loader = new FXMLLoader();
    try
    {
        root = loader.load(is);

    }
    catch (IOException ex)
    {
        System.out.println(ex);
    }

I hope it will help someone

Cheers :)

Upvotes: 0

Views: 1144

Answers (1)

mipa
mipa

Reputation: 10650

You can load FXML files from anywhere. One possibility is to use the load command which takes an URL as an argument. You can get that URL easily like this:

    try {
        File file = new File("../../FXMLDocument.fxml");
        if (file.canRead()) {
            URL url = file.toURI().toURL();
            Pane root = FXMLLoader.load(url);
        } else {
            System.err.println("File does not exist.");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

Upvotes: 1

Related Questions