Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 248
Dime Avatar asked Oct 13 '14 13:10

Dime


1 Answers

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();
    }
}
like image 85
Bernd Hochschulz Avatar answered Nov 03 '22 21:11

Bernd Hochschulz