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

那年仲夏 提交于 2019-11-29 06:46:56

问题


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:


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();
    }
}


来源:https://stackoverflow.com/questions/26341744/javafx-set-different-icons-for-the-title-bar-and-the-operating-system-task-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!