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.

 

ScrollBar parts CSS information

 

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 .

 

<?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)

 

/** 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

/* 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 .

/* 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.

/* 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 :)