Mike Croall
Mike Croall

Reputation: 55

JavaFX app icon fails to show 9/10 times

I have an application which opens absolutely fine, but am having trouble setting an icon for it. The icon I give the path to is there, and changing to another imagine in that directory shows the icon 9/10 times, but this image never shows. There is always a question mark in it's place. So even on another file, which I know will work (ie. isn't corrupted), how come it only shows so rarely?

Below is the code of MyApplication.java

package MyApp;

import MyApp.Variables.Constants;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Forms/FormMain.fxml"));

        primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/img/appicon.png")));
        primaryStage.setTitle("MyApp " + Constants.VERSION_NAME + " (" + Constants.RELEASE_ID + ")"); 
        primaryStage.setScene(new Scene(root, 1000, 800));
        primaryStage.show();
    }

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

Below is the project directory structure relating /img/ to Main.java:

Directory structure

I have tried all the solutions here but nothing fixed my issue.

Running on Ubuntu 16.04, intelliJ IDEA for the IDE, though the problem persists with an exported JAR file.

Upvotes: 1

Views: 1118

Answers (1)

aw-think
aw-think

Reputation: 4803

Loading Data from your disk is time consuming, so you be able to start loading the icon while the object is constructed. Place it in a constructor and save it in a instance member. Normally you need to add more than one icon, because each platform needs it own sizes (for links and so on).

package MyApp;

import MyApp.Variables.Constants;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Main extends Application {

    private Image icon;

    public Main() {
      icon = new Image(Main.class.getResource("/img/appicon.png").toExternalForm());
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Forms/FormMain.fxml"));

        primaryStage.getIcons().add(icon);
        primaryStage.setTitle("MyApp " + Constants.VERSION_NAME + " (" + Constants.RELEASE_ID + ")"); 
        primaryStage.setScene(new Scene(root, 1000, 800));
        primaryStage.show();
    }

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

My icon was this: enter image description here and the app structure in Netbeans looks like that:

enter image description here

and the running app look like that:

enter image description here

Upvotes: 3

Related Questions