How can i dynamically change the slider thumb icon image in a slider in javafx?

旧时模样 提交于 2019-12-12 11:06:26

问题


have slider in fxml file :

Slider fx:id="slider" minHeight="20" minWidth="-Infinity" prefWidth="300.0"

Want an slider thumb icon image to be change from my .java class as I can change the thumb icon image by using css

.slider .thumb{
 -fx-background-image :url("your image");   
 ...// more customization       
}

But I want to change the image from .java class

Please suggest..

Thanks


回答1:


You can find thumb from Java code using Node#lookup() method.

String IMAGE = getClass().getResource("my-image.png").toExternalForm();

StackPane thumb = (StackPane)slider.lookup(".thumb");
thumb.getChildren().clear();
thumb.getChildren().add(new ImageView(IMAGE));

N.B.: Note you need to call this code after stage.show() to have lookup working correctly.



来源:https://stackoverflow.com/questions/15926861/how-can-i-dynamically-change-the-slider-thumb-icon-image-in-a-slider-in-javafx

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