Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: Artifact displays empty window only

Preface: I am pretty new to JavaFX

Made an simple image resizing application and need to build an artifact of the application. Using IntelliJ and Eclipse (tried both), i built normal JAR artifacts and JavaFX artifacts. Both are executable, but only show an empty window.

When starting the application out of the IDE, there is no problem; the window containing all subpanes get displayed as they should.

Any ideas how to solve this problem? Thanks in advance!

Attached code of the application initialization part and a picture of the resulting windows - maybe it helps.

public class MainApp extends Application {

private Stage primaryStage;
private BorderPane rootLayout;

private SettingsViewController settingController;
private MainViewController mainViewController;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("SimpleImageResizer");

    this.primaryStage.getIcons().add(new Image("file:icon.png"));

    this.primaryStage.setOnCloseRequest(we -> ConfigurationCache.getInstance().pushConfig());

    targetResolutions.addAll(Arrays.asList(ResolutionPreset.values()));
    supportedFileFormats.addAll(Arrays.asList(SupportedFileFormat.values()));

    initRootLayout();

    showMainView();
    showSettingsView();
    primaryStage.setResizable(false);
}

public void initRootLayout() {
    try {
        // Load root layout from fxml file.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
        rootLayout = loader.load();

        // Show the scene containing the root layout.
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
.
.
.
public static void main(String[] args) {
    launch(args);
}

Artifact output

IDE output

like image 866
Christian Huber Avatar asked Dec 14 '22 13:12

Christian Huber


2 Answers

You didn't provide enough information to solve your problem, so here's an example that does work:

Create a package application and put this class into it:

application/Main.java

package application;

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) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/application/RootLayout.fxml"));
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

Create a "RootLayout.fxml" in the package application.

application/RootLayout.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
   <children>
      <Pane layoutX="-125.0" layoutY="-143.0" prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button layoutX="134.0" layoutY="161.0" mnemonicParsing="false" text="Button" />
         </children>
      </Pane>
   </children>
</AnchorPane>

In Eclipse select

Export -> Runnable JAR file -> Extract required libraries into generated JAR

(of course you need to specify the proper launch configuration)

The generated JAR can be executed and has the nodes from the fxml.

like image 173
Roland Avatar answered Dec 20 '22 18:12

Roland


loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));

try with

loader.setLocation(MainApp.class.getResource("/view/RootLayout.fxml"));

NetBeans the first variant will not compile in IDE

like image 26
user3224416 Avatar answered Dec 20 '22 20:12

user3224416