Java Reference
In-Depth Information
Understanding the
FXMLLoader
Now that we understand both the FXML file and the controller class that works with the FXML file, we turn our
attention to the loading of the FXML file at runtime. The
FXMLLoader
class in the
javafx.fxml
package does the bulk
of the work of loading FXML files. In our example, the FXML file loading is done in the
StageCoachMain
class.
Listing 3-3 shows the
StageCoachMain
class.
Listing 3-3.
StageCoachMain.java
package projavafx.stagecoach.ui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
import java.util.List;
public class StageCoachMain extends Application {
@Override
public void start(Stage stage) throws IOException {
final StageStyle stageStyle = configStageStyle();
FXMLLoader fxmlLoader = new FXMLLoader(StageCoachMain.class
.getResource("/projavafx/stagecoach/ui/StageCoach.fxml"));
Group rootGroup = fxmlLoader.load();
final StageCoachController controller = fxmlLoader.getController();
controller.setStage(stage);
controller.setupBinding(stageStyle);
Scene scene = new Scene(rootGroup, 250, 350);
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.setOnCloseRequest(we -> System.out.println("Stage is closing"));
stage.show();
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
stage.setX((primScreenBounds.getWidth() - stage.getWidth()) / 2);
stage.setY((primScreenBounds.getHeight() - stage.getHeight()) / 4);
}
public static void main(String[] args) {
launch(args);
}
Search WWH ::
Custom Search