Cześć.
Są to moje początki z JavąFX.
Mam pytanie jak zrobić, by w tym samym oknie pokazało się tak jakby nowe okno z nowym plikiem FXML.
Mój kod wygląda tak:
Main
package sample;
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
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXML/Main.fxml"));
primaryStage.setTitle("Application");
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
ControllerMain
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.layout.StackPane;
import java.io.IOException;
public class ControllerMain {
@FXML
private StackPane stackPane;
@FXML
void onApplication() throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("FXML/Application.fxml"));
stackPane.getChildren().add(root);
}
}
Main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<StackPane fx:id="stackPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerMain">
<children>
<Button fx:id="button" mnemonicParsing="false" onAction="#onApplication" text="Button" />
</children>
</StackPane>
Application.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label text="Label" />
</children>
</StackPane>
Tak jak to teraz zrobiłem, to nowy plik faktycznie się wyświetla, ale stare rzeczy nie znikają, mimo, że w Application.fxml jest nowy StackPane i Label dodany.