JAVAFX 2.0 How can i change the slider icon in a slider to an image?

后端 未结 4 1772
刺人心
刺人心 2021-01-02 19:04

I want to change the icon to my image, I\'ve looked through the CSS reference guide but I can\'t seem to find anything relevant. Is it even possible? Doesn\'t matter if it i

4条回答
  •  执念已碎
    2021-01-02 19:49

    Take a look at the sample code and images of how a custom slider is rendered in this AudioPlayer.

    Also the JFXtras library has numerous gauges if you just want feedback rather than an interactive control.

    Here is some sample css using the selector pointed out by invariant's answer. Note that I needed to add an -fx-padding specification at half the images dimensions in order for the whole image to display.

    /** slider.css
    place in same directory as SliderCss.java
    ensure build system copies the css file to the build output path */
    
    .slider .thumb {
        -fx-background-image :url("http://icons.iconarchive.com/icons/double-j-design/diagram-free/128/piggy-bank-icon.png");   
        -fx-padding: 64;
    }
    /* Icon license: creative commons with attribution: http://www.doublejdesign.co.uk/products-page/icons/diagram */
    

    Sample app:

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    
    public class SliderCss extends Application {
      public static void main(String[] args) { launch(args); }
      @Override public void start(Stage stage) throws Exception {
        VBox layout = new VBox();
        layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10px;");
        layout.getChildren().setAll(new Slider());
        layout.getStylesheets().add(getClass().getResource("slider.css").toExternalForm());
        stage.setScene(new Scene(layout));
    
        stage.show();
      }
    }
    

    Sample program output:

    piggy

提交回复
热议问题