JavaFX FXMLLoader load(URL location, ResourceBundle resources)

PreviousNext

JavaFX FXMLLoader load(URL location, ResourceBundle resources) Loads an object hierarchy from a FXML document.

Syntax

The method load() from FXMLLoader is declared as:

@SuppressWarnings("removal")
public static <T> T load(URL location, ResourceBundle resources) throws IOException

Parameter

The method load() has the following parameter:

  • URL location - the location used to resolve relative path attribute values
  • ResourceBundle resources - the resources used to resolve resource key attribute values

Return

The method load() returns the loaded object hierarchy

Exception

The method load() throws the following exceptions:

  • IOException - if an error occurs during loading

Example

The following code shows how to use JavaFX FXMLLoader load(URL location, ResourceBundle resources)

Example 1

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

public class FxmlMvcPatternDemoLauncher extends Application {
    public static void main(String[] args) throws ClassNotFoundException {
        Application.launch(FxmlMvcPatternDemoLauncher.class, args);
    }/*w    ww    . d e   m o  2   s .  c   o   m*/

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(FxmlMvcPatternDemoLauncher.class.getResource("MainView.fxml"), ResourceBundle
                .getBundle(FxmlMvcPatternDemoLauncher.class.getPackage().getName() + ".MainView")/*properties file*/
        );

        stage.setScene(new Scene(root));
        stage.show();
    }
}

Example 2

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

import java.util.ResourceBundle;

public class Main extends Application {

    @Override//  w    w  w .  d   em  o    2 s   .c  o   m 
    public void start(Stage primaryStage) throws Exception {
        ResourceBundle res = ResourceBundle.getBundle("foo");
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"), res);
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }

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

Example 3

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import java.awt.*;
import java.io.IOException;

/**//   w  w   w  .d    em  o  2   s  . c   o   m
 
 */
public class formCallController {

    public GridPane loadChild(GridPane pane, String location) {

        try {
            pane = FXMLLoader.load(getClass().getResource(location), null);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return pane;
    }
}
PreviousNext

Related