Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Slider change width (thickness)

My problem is that the default Slider is too thin. Is there a way to make the Slider (width) bigger?

Thanks for advice.

like image 235
heky__ Avatar asked Jan 06 '23 17:01

heky__


2 Answers

In JavaFX most of the styling can be done via CSS.

You can use the .slider .track and the .slider .thumb style classes in your CSS file. In this style classes you can use the -fx-pref-width and -fx-pref-height attributes:

.slider .track {
    -fx-pref-height:20;
}

.slider:vertical .track {
    -fx-pref-width:20;
}

.slider .thumb {
    -fx-pref-height: 30;
    -fx-pref-width: 30;
}

Or alternatively you can use the -fx-padding attribute:

.slider .track {
    -fx-padding: 10;
}

.slider .thumb {
    -fx-padding: 15;
}

This will produce horizontal and vertical Sliders like these:

enter image description here

In any case, you can refer to the JavaFX 8 default stylesheet (modena.css) and the JavaFX CSS Reference Guide.

You can also use the official tutorial about styling JavaFX with CSS.

like image 160
DVarga Avatar answered Jan 17 '23 21:01

DVarga


Because I spent way to much time researching a similar problem, here is how you get rid of the track and it's "clickability":

CSS:
.slider .track {
    visibility: hidden;
}

// You'll probably want this in the code also to swallow all keyboard events:

slider.addEventFilter(KeyEvent.ANY, new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent event) {
        System.out.println("KeyEvent NO!");
        event.consume();
    }
});
like image 36
user2683474 Avatar answered Jan 17 '23 20:01

user2683474