JavaFX ScrollBar
In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in JavaFX, javafx.scene.control.ScrollBar class is used.
JavaFX Horizontal ScrollBar:
Example:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.StackPane;
public class ScrollBarUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
ScrollBar newcroll = new ScrollBar();
StackPane pane = new StackPane();
pane.getChildren().add( newcroll );
Scene scene = new Scene( pane, 400, 200 );
primaryStage.setScene( scene );
primaryStage.setTitle(" JavaFX horizontal ScrollBar Example ");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the ScrollBar in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene, javafx.stage.Stage, javafx.scene.control.ScrollBar, javafx.scene.layout.StackPane.
Then we have created one class named ScrollBarUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold horizontal ScrollBar, a StackPane object is created which is then passed to the Scene class object.
The ScrollBar is created with its constructor.
The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX horizontal ScrollBar Example”. Also, it displays Simple ScrollBar covering all horizontal space.
JavaFX Vertical ScrollBar:
Example:
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.StackPane;
public class ScrollBarUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
ScrollBar newcroll = new ScrollBar();
newcroll.setOrientation(Orientation.VERTICAL);
StackPane pane = new StackPane();
pane.getChildren().add( newcroll );
Scene scene = new Scene( pane, 400, 200 );
primaryStage.setScene( scene );
primaryStage.setTitle(" JavaFX vertical ScrollBar Example ");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the vertical ScrollBar in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene, javafx.stage.Stage, javafx.scene.control.ScrollBar, javafx.scene.layout.StackPane.
Then we have created one class named ScrollBarUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold vertical ScrollBar, a StackPane object is created which is then passed to the Scene class object.
The ScrollBar is created with its constructor. And then setOrientation() method with Orientation. The VERTICAL argument is called on scroll bar object.
The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX vertical ScrollBar Example”. Also, it displays vertical ScrollBar covering all vertical space.
JavaFX ScrollBar with min, max and current value:
Example:
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.StackPane;
public class ScrollBarUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
ScrollBar newcroll = new ScrollBar();
newcroll.setMin(0);
newcroll.setMax(200);
newcroll.setValue(80);
newcroll.setOrientation(Orientation.VERTICAL);
newcroll.setUnitIncrement(10);
newcroll.setBlockIncrement(10);
StackPane pane = new StackPane();
pane.getChildren().add( newcroll );
Scene scene = new Scene( pane, 400, 200 );
primaryStage.setScene( scene );
primaryStage.setTitle(" JavaFX vertical ScrollBar with current value specified Example ");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the vertical ScrollBar with current value specified in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene, javafx.stage.Stage, javafx.scene.control.ScrollBar, javafx.scene.layout.StackPane, javafx.geometry.Orientation.
Then we have created one class named ScrollBarUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold vertical ScrollBar with the current value, min, max value specified, StackPane object is created which is then passed to Scene class object.
The ScrollBar is created with its constructor. And then setOrientation() method with Orientation. The VERTICAL argument is called on scroll bar object. The setMin(), setMax(), setValue() method is called with value passed to it to set min, max and current value specified.
The stage is prepared, the title is set and the show() method is called to display output. The launch(args) method is called. In output Frame like container is displayed with a title, "JavaFX vertical ScrollBar with the current value specified Example”. Also, it displays vertical ScrollBar covering all vertical space and current value specified to 80 unit.