JavaFX FXMLLoader setLocation(URL location)

PreviousNext

JavaFX FXMLLoader setLocation(URL location) Sets the location used to resolve relative path attribute values.

Syntax

The method setLocation() from FXMLLoader is declared as:

public void setLocation(URL location)

Parameter

The method setLocation() has the following parameter:

  • URL location - the location

Example

The following code shows how to use JavaFX FXMLLoader setLocation(URL location)

Example 1

import java.io.IOException;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;

public class JfxUtils {

    public static Node loadFxml(String fxml) {
        FXMLLoader loader = new FXMLLoader();
        try {
            loader.setLocation(JfxUtils.class.getResource(fxml));
            Node root = (Node) loader.load(Main_SEAFILE.class.getResource(fxml).openStream());
            return root;
        } catch (IOException e) {
            throw new IllegalStateException("cannot load FXML screen", e);
        }
    }

}

Example 2

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

public class Main extends Application {

    @Override//  w    w  w .   d e   m o 2  s  .   c   o  m
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("View.fxml"));
        Scene scene = new Scene(loader.<Parent>load());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

Example 3

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

public class Main extends Application {
    @Override//   w   w w .  d    em  o   2 s  .    co  m  
    public void start(Stage primaryStage) {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("/view/MortgageForm.fxml"));
            primaryStage.setScene(new Scene(loader.load()));
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

Related