JavaFX Using an Alternative FXML Structure for Title (Stage is root)

I read the answer about including Title in FXML ( JavaFx: Set window title in fxml file ), but I don’t understand how to call this code.

I can not call it the classic way:

FXMLLoader loader = new FXMLLoader(getClass().getResource("some.fxml")); Scene scene = new Scene(loader.load()); Stage stage = new Stage(); stage.initOwner(root.getScene().getWindow()); stage.initModality(Modality.WINDOW_MODAL); stage.setScene(scene); stage.show(); 

some.fxml

 <?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.VBox?> <?import javafx.stage.Stage?> <?import javafx.scene.Scene?> <?import javafx.scene.control.Label?> <Stage title="Some Stage"> <scene> <Scene> <VBox xmlns:fx="http://javafx.com/fxml"> <children> <Label text="John Doe"/> </children> </VBox> </Scene> </scene> </Stage> 
0
source share
1 answer

Since fxml creates the scene, you don’t need to create another step in your Java code, just get a link to the scene created by FXML and show it directly.

titled stage via fxml

StageLoader.java

 import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.*; import java.io.IOException; public class StageLoader extends Application { private void showDialog(Stage owner) { try { FXMLLoader loader = new FXMLLoader( getClass().getResource("some.fxml") ); Stage dialog = loader.load(); dialog.initOwner(owner); dialog.initModality(Modality.WINDOW_MODAL); dialog.initStyle(StageStyle.UTILITY); dialog.show(); } catch (IOException e) { System.out.println("Unable to load dialog FXML"); e.printStackTrace(); } } @Override public void start(final Stage stage) throws IOException { Button openDialog = new Button("Open Dialog"); openDialog.setOnAction(event -> showDialog(stage)); stage.setTitle("Main Window"); stage.setScene( new Scene( new StackPane(openDialog), 200, 200 ) ); stage.show(); } public static void main(String[] args) { launch(args); } } 

I made a few minor changes to fxml to ensure that the resulting stage is large enough to actually see the name of the dialog stage.

some.fxml

 <?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.VBox?> <?import javafx.stage.Stage?> <?import javafx.scene.Scene?> <?import javafx.scene.control.Label?> <Stage title="Some Stage" resizable="false" xmlns:fx="http://javafx.com/fxml" > <scene> <Scene> <VBox > <children> <Label text="John Doe" prefWidth="150"/> </children> </VBox> </Scene> </scene> </Stage> 

SceneBuilder that cannot open FXML after adding Stage and Scene tags.

You can write FXML with definitions of the scene and scene as an external shell with the built-in <fx:include..> operator to include an internal FXML document that could be opened, edited directly in SceneBuilder. In addition, you can create a feature request against SceneBuilder (it is called a “design tool” in the problem tracker) to request direct support for FXML files with stages and scenes included in FXML.

0
source

Source: https://habr.com/ru/post/952002/


All Articles