简体   繁体   中英

is it possible to change the icon after start in JavaFx

I have a program that I want the icon to change depending on which area of the app you open. So the default icon would be a rainbow. if you click the green button the icon changes to green. if you click blue it changes blue. Simple if possible but I can't find any solution out there, I only find answers to how to change the default icon.

public static Scene scene;
public static Stage stage1;
public void start(Stage stage) throws IOException {
    scene = new Scene(loadFXML("ChooseYourColor"));
    stage.setTitle("Rainbow-window");
    stage.setScene(scene);
    stage1 = stage;
    stage.getIcons().add(new Image(TBA.class.getResourceAsStream("RainbowIcon.png")));
    stage.show();
}
static void setRoot(String fxml) throws IOException {
    scene.setRoot(loadFXML(fxml));
}
private static Parent loadFXML(String fxml) throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
    return fxmlLoader.load();
}
@FXML
protected void ChangeBlue() throws IOException {
        App.setRoot("Blue-Window");
        Stage primStage = App.stage1;
        primStage.setTitle("Blue");
        primStage.getIcons().add(new Image(App.class.getResourceAsStream("BlueIcon.png")));
    }
}

So what I did was I made a collection of Images called iconsList in main adding the images with

iconsList.add(new Image(TBA.class.getResourceAsStream("Logo.png")));
iconsList.add(new Image(TBA.class.getResourceAsStream("AMFLogo.png")));

then in start I did setAll and it takes index 0 as the initial

stage.getIcons().setAll(iconsList);

Then on a button call, I changed it with

primStage.getIcons().set(0, App.iconsList.get(1));

Hope this can help someone else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM