JavaFX Scroll Bar

0

JavaFX Scroll Bar tutorial :

The scroll bar that lets the user scroll the contents of a panel or other region.
Mainly it is used with ScrollPane and ListView.

Scroll bar has two types(Orientation) :

  • Horizontal ScrollBar
  • Vertical ScrollBar

Constructors :

ScrollBar()
Creates a new horizontal ScrollBar

Methods used in scroll bar:

  • setOrientation() is used to set orientation AS vertical or horizontal.
  • setMin() to set minimum value of ScrollPane.
  • setMax() to set maximum value of ScrollPane.
  • setValue() to set value of ScrollPane.

Example of Scroll Bar:


package javafxtuts;

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
 *
 * @author javafxtuts
 */
public class Javafxtuts extends Application {

  @Override
  public void start(Stage primaryStage) {
      
      // setting title of application
      primaryStage.setTitle("javafxtuts.com");
      VBox root=new VBox();
      
      ScrollBar sp =new ScrollBar();
      ScrollBar sp1 =new ScrollBar();
      //setting orientation
      sp.setOrientation(Orientation.HORIZONTAL);
      sp1.setOrientation(Orientation.VERTICAL);
      //seting min, max values 
      sp.setMax(100.0);
      sp.setMin(0.0);
      sp.setValue(20);
	//to add nodes to VBox Layout(root)
      root.getChildren().addAll(sp,sp1);
      Scene scene = new Scene(root, 350, 175);
      primaryStage.setScene(scene);
      primaryStage.show();
  }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

Output :

Explanation of Scroll Bar example :

    • To creating ScrollBar
      ScrollBar sp =new ScrollBar();
    • To make it horizontal we use setOrientation() method As
      sp.setOrientation(Orientation.HORIZONTAL);
    • sp.setMax(100.0);
      It set maximum value 100.0 of sp.
    • sp.setmin(0.0);
      It set maximum value 0.0 of sp.
  • sp.setvalue(20.0);
    It set value 20.0 of sp.
Share.

Leave A Reply