Issue
I have made theme selector in menu bar in javaFx program. I made two menu items using radiomenuitem when select on any of the items then event handler fire and apply css according to their id if id = 1 then theme1.css will be apply like that.
(Note : this is controller class and I set scene and stage from main.java using setScene() and setStage() by default theme1.css is applied )
now when I select item2 then theme2.css apply but when I again select item1 its not reverting or not apply theme1.css
when I print on console then path appears correct.
so why its not applying css again to same scene?
do I have to refresh scene? or something I am missing .
I noticed one thing that --- if I have three items
Item 1 /
Item 2
Item 3
and Item 1 is selected then theme1.css will be apply and when I select Item 2 then theme2.css will be apply and when I select Item 3 then theme3.css will be apply but now when I again select either Item 1 or Item 2
Then nothing changes :(
private RadioMenuItem[] themes = new RadioMenuItem[]{new RadioMenuItem("Theme 1"),new RadioMenuItem("Theme 2")};
private String menuid;
private Stage stage;
private Scene scene;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
ToggleGroup myToggleGroup = new ToggleGroup();
themes[0].setToggleGroup(myToggleGroup);
themes[1].setToggleGroup(myToggleGroup);
themes[0].setId("1");
themes[1].setId("2");
themes[0].setSelected(true);
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
menuid = ((RadioMenuItem) e.getSource()).getId();
if (((RadioMenuItem)e.getSource()).isSelected()) {
scene.getStylesheets().add(getClass().getResource("/themes/theme"+menuid+".css").toExternalForm());
System.out.println("/themes/theme"+menuid+".css");
System.out.println(scene);
System.out.println(stage);
stage.setScene(scene);
System.out.println("scene sceee");
}
}
};
themes[0].setOnAction(event);
themes[1].setOnAction(event);
menutheme.getItems().addAll(themes);
}
public void setStage(Stage primarystage){
stage = primarystage;
}
public void setScene(Scene primaryscene){
scene = primaryscene;
}
Solution
Ok now I have solved that issue
First I should clear old stylesheet using clear()
like this
scene.getStylesheets().clear();
Answered By - Gaurav_Solo
Answer Checked By - Marie Seifert (JavaFixing Admin)