How can i dynamically change the slider thumb icon

2019-06-23 02:51发布

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条回答
We Are One
2楼-- · 2019-06-23 02:59

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.

查看更多
登录 后发表回答