JavaFX. Set different icons for the title bar and the operating system task bar


Question

Is there a way in JavaFX to set different application icons for the title bar and for the operating system task bar?

The problem is that the icon shown in the system task bar is much bigger compare to the icon in the title bar and they cannot be re-sized properly by the system.

I would like to use different images for the different icon sizes. Similar to what you do in an .ico file.

If I call stage.getIcons().add(...) two times, the former image will be always used for both bars.

I was also not able to use an already existing .ico file (that supports different sizes) for this purposes.

1
14
10/13/2014 1:55:45 PM

There is a way by using two different stages but it may have it's problems (only tested on Windows 7). The following example uses Java 8/JavaFX 8.

This code sets the icon that is shown on the taskbar on the primary stage received on JavaFX startup but makes the stage invisible (transparent, zero size). It then opens a new and visible window with a different icon.

Since this is only a child window and not the real one, the hide event has to be delegated to the hidden stage to close the application. There may be more events that have to be delegated like minimizing the window.

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.getIcons().add(getImage("taskbar_icon.png"));
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setWidth(0);
        primaryStage.setHeight(0);
        primaryStage.show();

        Stage visibleStage = new Stage();
        visibleStage.initOwner(primaryStage);
        visibleStage.getIcons().add(getImage("window_icon.png"));
        visibleStage.setScene(new Scene(...));
        visibleStage.setOnHidden(e -> Platform.runLater(primaryStage::hide));
        visibleStage.show();
    }
}
2
11/3/2014 8:57:41 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon