Hello friends,
I came today with new stuffs of customizing the scrollbar control in JavaFX after long duration. 🙂
First Let’s look over what are the parts of ScrollBar as given in below image below.
In the above picture there are four main part of ScrollBar .
- increment
- thumb
- track
- decrement
Now in this post we are going to just play with only these four style class and at the right of the image above there are some of the events like hover(focus), pressed(mouse pressed) , horizontal(Horizontal specific ScrollBar CSS class) and vertical (Vertical specific ScrollBar CSS class) .
Firstly we are making one demo Application which contain ListView and one Stylesheet .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.net.*?> <?import java.util.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.paint.*?> <AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml"> <children> <ListView id="listView1" layoutX="47.0" layoutY="38.0" prefHeight="200.0" prefWidth="200.0" styleClass="mylistview" /> <ListView id="listView2" layoutX="281.0" layoutY="39.0" orientation="HORIZONTAL" prefHeight="200.0" prefWidth="200.0" styleClass="mylistview" /> </children> <stylesheets> <URL value="file:/C:/style.css" /> </stylesheets> </AnchorPane> |
In above FXML we have created two listview with style class naming mylistview. Now we will be using same style class on all over our stylesheets (style.css) Let’s make the scrollbar like below:
Css file (style.css)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
/** CSS **/ .mylistview{ -fx-border-color:derive(gray,80%); } /* The main scrollbar CSS class of ListView */ .mylistview .scroll-bar:horizontal , .mylistview .scroll-bar:vertical{ -fx-background-color:transparent; } /* The increment and decrement button CSS class of scrollbar */ .mylistview .increment-button ,.mylistview .decrement-button { -fx-background-color:transparent; -fx-border-color:derive(gray,80%); } /* The main scrollbar **track** CSS class */ .mylistview .scroll-bar:horizontal .track , .mylistview .scroll-bar:vertical .track{ -fx-background-color: transparent; -fx-border-color:derive(gray,80%); -fx-background-radius: 0em; } /* The main scrollbar **thumb** CSS class which we drag every time (movable) */ .mylistview .scroll-bar:horizontal .thumb, .mylistview .scroll-bar:vertical .thumb { -fx-background-color:derive(black,90%); -fx-background-insets: 0, 0, 0; -fx-background-radius: 0em; } /* ------------------------------------------------------------------------------------- */ /** EVENT CSS **/ /* ------------------------------------------------------------------------------------- */ /* The main scrollbar **track** CSS class on event of "hover" and "pressed" */ .mylistview .scroll-bar:horizontal:hover .track , .mylistview .scroll-bar:horizontal:pressed .track , .mylistview .scroll-bar:vertical:hover .track, .mylistview .scroll-bar:vertical:pressed .track{ -fx-background-color: derive(#434343,20%); -fx-opacity: 0.2; -fx-background-radius: 0em; } /* The main scrollbar **thumb** CSS class on event of "hover" and "pressed" */ .mylistview .scroll-bar .thumb:hover, .mylistview .scroll-bar .thumb:pressed{ -fx-background-color: derive(black,50%); } .mylistview .increment-button:hover ,.mylistview .decrement-button:hover { -fx-background-color:derive(gray,100%); -fx-border-color:derive(gray,80%); -fx-padding:10px; } |
Here we have used following property:
- -fx-padding: for giving padding space in between the class block
- -fx-border-color: this give the color for the border of the class
- -fx-background-radius: makes the background fill radius
- -fx-background-color: fills the background with the color like red,gree, blue etc.
- -fx-opacity: the opacity of the class between [0.0-1.0]
- -fx-background-inset: it’s the insets spaces of specific class.
Now how would you like if you want your scrollbar little bit wider and bigger than it’s default size (like of below image) . To make them wider you can just add -fx-padding property like as below CSS code.
Just override your existing CSS (style.css) codes with this few lines of codes
1 2 3 4 5 6 |
/* The increment and decrement button CSS class of scrollbar */ .mylistview .increment-button ,.mylistview .decrement-button { -fx-background-color:transparent; -fx-border-color:derive(gray,80%); -fx-padding:10px; } |
To make the scrollbar little bit small and curvy as well as the arrow button available which is given below can be done by overriding your existing style.css with below codes .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/* The increment and decrement button CSS class of scrollbar */ .mylistview .increment-button ,.mylistview .decrement-button { -fx-background-color:transparent; -fx-background-radius: 2em; } /* The main scrollbar **track** CSS class */ .mylistview .scroll-bar:horizontal .track, .mylistview .scroll-bar:vertical .track{ -fx-background-color: transparent; -fx-border-color:derive(gray,80%); -fx-background-radius: 2em; -fx-border-radius:2em; } /* The main scrollbar **thumb** CSS class which we drag every time (movable) */ .mylistview .scroll-bar:horizontal .thumb, .mylistview .scroll-bar:vertical .thumb { -fx-background-color:derive(black,90%); -fx-background-insets: 2, 0, 0; -fx-background-radius: 2em; } |
How about removing all those arrows, buttons of increment and decrement i.e only thumb in scrollbar. To accomplish this logic you need to add CSS codes in existing style.css.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* The main scrollbar **track** CSS class */ .mylistview .scroll-bar:horizontal .track, .mylistview .scroll-bar:vertical .track{ -fx-background-color:transparent; -fx-border-color:transparent; -fx-background-radius: 0em; -fx-border-radius:2em; } /* The increment and decrement button CSS class of scrollbar */ .mylistview .scroll-bar:horizontal .increment-button , .mylistview .scroll-bar:horizontal .decrement-button { -fx-background-color:transparent; -fx-background-radius: 0em; -fx-padding:0 0 10 0; } /* The increment and decrement button CSS class of scrollbar */ .mylistview .scroll-bar:vertical .increment-button , .mylistview .scroll-bar:vertical .decrement-button { -fx-background-color:transparent; -fx-background-radius: 0em; -fx-padding:0 10 0 0; } .mylistview .scroll-bar .increment-arrow, .mylistview .scroll-bar .decrement-arrow, { -fx-shape: " "; -fx-padding:0; } /* The main scrollbar **thumb** CSS class which we drag every time (movable) */ .mylistview .scroll-bar:horizontal .thumb, .mylistview .scroll-bar:vertical .thumb { -fx-background-color:derive(black,90%); -fx-background-insets: 2, 0, 0; -fx-background-radius: 2em; } |
Here we are using one more new property of CSS:
- -fx-shape : In this property we can keep the SVG shape code inside a String . CSS can take those SVG path
Other More Stylable ScrollBars
This the another CSS styled Scrollbar where I’ve used my own custom Shapes on scrollbar thumb class
And this final one Listview contains the image in the scrollbar which looks lot more fancier than ever. Also I’ve used the gradient color at the middle of the scrollbar’s thumb. It looks lot more easier to customize as you like.
It’s really amazing to play with CSS there are even more things to do with CSS in javafx. For more information of the CSS reference there is a link given below
All the source codes of the CSS is available in below zip file with screenshots as well.
CSS Files: 6 css scrollbar files
CSS Reference: http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html
Thanks guys for giving interest in this post. Please do feedback about your feelings and thoughts. See you soon 🙂
Hello, Narayan!
I have a problem with build 2.2.16 and this line:
Error message is “URL is not a valid type”
If ‘URL’ change to ‘url’ – another error : “Invalid property”
Could you suggest reasons?
WBR, Anatoly!
Sorry, forgot pointed to line:
Hi anatolnsk,
If it’s happening from FXML then please use coding wise to add CSS Stylesheet instead of from FXML
like below
scene.getStylesheets().add("file:\C:\style.css"); // or from relative path
Thanks
Oh, it really works! ( Is it a bug with url from fxml ? )
I only populated listviews with labels and now it certainly rocks!
Tnx for blog!
Pingback: Java desktop links of the week, July 16 | Jonathan Giles
very nice
thanks 🙂
i want to know how it will be apply on HTML???what will be source code write on html page??please tell me about it..i want to know it.
Hi shano,
I’ve not yet done that on real HTML. But as i know I’ve seen in facebook scrollbar (Timeline Scrollbar) they are making their own scrollbar ie. there are Two scroll bar one is their own and other is the browser default. They actually hide the browser default scrollbar and shows their own scrollbar using the Javascript.
Thanks
Hey, thanks for the CSS magic. Do you have by any chance SplitPane related CSS setup? I managed to use -fx-shape to get rid of handler, but have no idea how to make it as thin as possible. Like in iTunes or JavaFX Scene Builder itself
Hi @Nick,
For making your splitpane as think as possible. You need to first set the class of the splitpane and edit via CSS.
Let’s suppose your splitpane user-defined css class is set to “mysplit” then to make your splitpane very thin you can use this css.
CSS CODE:
I think it will solve your problem.
Thanks
Narayan
Thank you @Narayan. That did the trick!
I kept changing horizontal-grabber style and split-pane and completely missed split-pane-divider
Where do you get all this knowledge? The css reference doesn’t help me much at my current JavaFX stage
http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html
hi @Nick,
You must be good in CSS coder for this.
Refer to w3c : http://www.w3.org/TR/CSS2/selector.html#child-selectors
which helps to know the selector i’ve used. And the javafx css reference is quiet good.You can also refer to caspian.css which is the core css of the javafx.
Thanks.
Hi, Nice blog!!
Based on your blog, now I can customize the scroll bar for my application.
But, I can’t have the couple of horizontal lines (thumbnails ?) inside thumb of my scrollbar…
Can you please give me any comments on this ?
@Jay
To add the lines inside the thumb you can choose either to use with the SVG or background tricks. I’ve already attached the source of all scrollbar shown in the post. May be this line will help you.
/* Add this in your thumb class */
-fx-background-color:
linear-gradient( to bottom,
-mycolor 46%,
-mycolor2 47%,
-mycolor 48%,
-mycolor2 49%,
-mycolor 50%,
-mycolor2 51%,
-mycolor 52%,
-mycolor2 53%,
-mycolor 54%
)
Pingback: JavaFX | Annotary
Very interesting post ! Keep going !
Hi is there any way to set the width/height of a scrollbar ‘s thumb ?
Because i want to set a custom image in the thumb and its not fit well
Hi Nilos,
Although you cannot set exact height and width but you can somehow manage to increase the width and height using
-fx-padding, -fx-background-insets, -fx-border-insets
Thanks
How to add scrolling action using scene builder.
We’ve 3 scrolling related options in “Code” section.
So how to add scrolling using them.
I didn’t get it. Can you elaborate it?
very longan crowded code for scroolbar.İs ther short code for scroolbar?
I can add your minified css 🙂 I am not a professional in CSS so they might be longer codes right now.
Good blog Narayan.
I am trying to display the vertical scroll bar in TableView all the time instead of only when overflow occurs. The following doesn’t work:
.tableView .scroll-bar:vertical { -fx-visibility: visible; }
Can you help me out? Thanks
Excellent post. Keep writing such kind of information on your site.
Im really impressed by your site.
Hey there, You’ve performed an excellent job. I will definitely digg it and for my part suggest to my
friends. I’m sure they’ll be benefited from this website.
Great post. Helped a lot. Thanks 🙂
Exceptional post but I was wondering if you could write a
litte more on this subject? I’d be very grateful if you could elaborate a little bit more.
Many thanks!
Thanks for giving details I could find no where else, particularly how to style the TextArea scroll bar thumb. But, isn’t it a mistake to refer to the TextArea by the name of the reference variable when preceded by a period? My understanding is that when the identifier is preceded by a period it must be a modified name of the class, i.e. “text-area”.