Forum Stats

  • 3,826,757 Users
  • 2,260,705 Discussions
  • 7,897,072 Comments

Discussions

[FX] integrate FX dialog into Swing App fails loading fxml

TPD-Opitz
TPD-Opitz Member Posts: 2,465 Silver Trophy
edited Oct 29, 2015 10:03AM in Java 8 Questions

Hello,

I'm about to migrate my Swing application and I'd like to start by adding new Dialogs as FX-GUI.

I found this (german) website explaining how to do that:

https://jaxenter.de/wie-man-swing-anwendungen-mit-javafx-modernisiert-25212

But the sithe does not work with FXML defined scene.

My Test applicarion throws an exception and I cant figure out why:

javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.
/D:/data/scm-workspace/anderes/Test/bin/tpd/application/fx/ExampleDialog/ExampleDialog.fxml:9
    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$RootElement.constructValue(FXMLLoader.java:1326)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at tpd.application.fx.MyFxmlLoaderImpl.loadFor(MyFxmlLoaderImpl.java:19)
    at tpd.application.fx.JFXHiddenApplication.loadFor(JFXHiddenApplication.java:94)
    at tpd.application.fx.AbstractFxDialog.<init>(AbstractFxDialog.java:15)
    at tpd.application.fx.ExampleDialog.ExampleDialog.<init>(ExampleDialog.java:15)
// ...

Complete stacktrace attached...

I'm a little confused since I set the root immediately before calling the FXMLLoader:

public abstract class AbstractFxDialog extends Stage {
    public AbstractFxDialog(MyFxmlLoader fxmlLoader) {
        Scene scene = new Scene(new Pane(), 1, 1);
        setScene(scene);
        scene.setRoot(new Pane()); // line 14
        Pane pane = fxmlLoader.loadFor(getClass()); // failing line

I attached a SSCCE to play with.

Could someone please help me solving this?

bye

TPD

Best Answer

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Oct 29, 2015 10:03AM Answer ✓

    The solution was to register a controller class and to change the root element in the fxml from <root ...> to <BorderPane...> (or any valid FX container element).

    bye

    TPD

    TPD-Opitz

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Oct 29, 2015 10:03AM Answer ✓

    The solution was to register a controller class and to change the root element in the fxml from <root ...> to <BorderPane...> (or any valid FX container element).

    bye

    TPD

    TPD-Opitz