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

1

16

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.

Paschasia answered 13/10, 2014 at 13:55 Comment(1)
Instead of calling stage.getIcons().add() more than once, Try setting all your images at once. stage.getIcons().addAll(Image1, Image2, Image3);Olio
S
3

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();
    }
}
Suggestibility answered 3/11, 2014 at 20:57 Comment(1)
Unfortunately the iconified property of the primary stage does not work anymore if the StageStyle is set to TRANSPARENT. So if you click the taskbar icon on windows you cannot react on this event on the primary stage and so the visibleStage stays maximized even if you want it to be minimized to the task bar.Cuman

© 2022 - 2024 — McMap. All rights reserved.