How to create a ScrollBar using JavaFX?


A scroll bar contains a thumb, right and left buttons attached to a scrollable pane. Using this you can scroll the pane (attached to it) up and down.

In JavaFX the javafx.scene.control.ScrollBar represents a scrollbar. You can create a scroll bar instantiating this class.

You can create either a vertical or a horizontal scroll bar, by default a horizontal scroll bar is created, you can change it to vertical using the setOrientation() method.

Usually, a scroll bar is associated with other controls such as ScrollPane, ListView, etc.

Example

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class ScrollBarExample extends Application {
   public void start(Stage stage) {
      //Label for education
      Label label = new Label("Educational qualification:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //list View for educational qualification
      ScrollBar scroll = new ScrollBar();
      scroll.setMin(0);
      scroll.setMax(400);
      scroll.setValue(50);
      //Setting the position of the scroll pane
      scroll.setLayoutX(180);
      scroll.setLayoutY(75);
      //Setting the stage
      Group root = new Group(scroll);
      Scene scene = new Scene(root, 595, 200, Color.BEIGE);
      stage.setTitle("List View Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 18-May-2020

664 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements