Updated April 11, 2023
Introduction to Javafx Scrollpane
The JavaFX Scroll Pane control is a container and, it has 2 scroll bars around the UI component in horizontal and vertical directions, if at all if the component is larger than the visible area of the Scroll Pane area. These scroll bars are enabling the user to scroll around the UI component displayed inside the scroll pane, so we can able to see different area parts of the component can be viewed. The JavaFX package is available in javafx.scene.control.ScrollPane package.
Advantage
Scroll pane is allowed to view complete elements by vertical and horizontal scroll.
Constructor
ScrollPane(): It is a default constructor, used to create a scroll pane instance.
Frequently used Methods
- setContent(): setContent() method in JavaFX used with ScrollPane for set any content to the ScrollPane instance.
- setPannable(true): setPannable(true) method in JavaFX used with ScrollPane for preview the image by clicking it and moving cursor. By default setPannable() is false.
- setPrefSize(): setPrefSize()method in JavaFX used with ScrollPane for set the preferable size to the scroll pane.
- setVbarPolicy(): setVbarPolicy()method in JavaFX used with ScrollPane for set the vertical policy for scroll pane.
- setHbarPolicy(): setHbarPolicy()method in JavaFX used with ScrollPane for set the horizontal policy for scroll pane.
How to Create ScrollPane in JavaFX?
- Accessing JavaFX features user-defined class must extend Application class. In JavaFX creating ScrollPane is the first step. ScrollPane can instantiate by using the new keyword.
Syntax:
ScrollPane scrollPane=new ScrollPane();
- Adding content or scroll bar policies or setPannable properties to the scrollPane is the second step.
Syntax:
scrollPane.setContent();
scrollPane.setVbarPolicy();
scrollPane.setHbarPolicy();
- Create Tile Pane or any other display(like HBox or VBox etc. as per requirement) class to add the items is the third step.
Syntax:
TilePane tPane=new TilePane (); //Gives horizontal box
- Creating a scene means screen to display output is the fourth step.
Syntax:
Scene screen = new Scene(tPane, length, width);
- Adding a Scene reference screen to the Stage object reference is the fifth step. Adding output screen to Stage. We will get this stage object reference from the start predefined JavaFX method.
Syntax:
stage.setScene(screen);
- At last display output screen by showing stage reference with the show () method.
Syntax:
stage.show();
Examples of Javafx Scrollpane
Here are the following examples mention below
Example #1
Adding label content to the ScrollPane and Vertical Scroll Bar Example
JavaFX Code:
package com.scrollpane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ScrollPaneVerticlaScrollBar extends Application {
@Override
public void start(Stage primaryStage) {
// setting the title of application
primaryStage.setTitle("ScrollPane Vertical");
// Create a ScrollPane
ScrollPane scrollPane = new ScrollPane();
//creating labels for adding content
Label labelContent = new Label(
"EDUCBA is online teaching platform for IT courses like Java, HTML, CSS");
//setting preferable size(height and width) for the label content
labelContent.setPrefSize(400, 100);
Label labelContent1 = new Label(
"EDUCBA is teach you every concept in simple and clear manner");
labelContent1.setPrefSize(400, 100);
Label labelContent2 = new Label(
"EDUCBA is offer you big discount for every course");
labelContent2.setPrefSize(400, 100);
//creating VBox for adding all labels
VBox vBox=new VBox();
vBox.getChildren().add(labelContent);
vBox.getChildren().add(labelContent1);
vBox.getChildren().add(labelContent2);
// Setting the content to the ScrollPane
scrollPane.setContent(vBox);
// Always show vertical scroll bar for scrolling
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
//adding scroll pane to the scene
Scene scene = new Scene(scrollPane, 551, 201);
primaryStage.setScene(scene);
//showing the output
primaryStage.show();
}
public static void main(String[] args) {
//invoking main method from JVM
launch(args);
}
}
Output:
Explanation:
- We have added label content to the VBox and VBox to the ScrollPane.
- We can see in the above output, content is scrolling vertical direction because of setVPolicy().
Example #2
Adding label content to the ScrollPane and Horizontal Scroll Bar Example
JavaFX Code:
package com.scrollpane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ScrollPaneHorizontalScrollBar extends Application {
@Override
public void start(Stage primaryStage) {
// setting the title of application
primaryStage.setTitle("ScrollPane Vertical");
// Create a ScrollPane
ScrollPane scrollPane = new ScrollPane();
//creating labels for adding content
Label labelContent = new Label(
"EDUCBA is online teaching platform for IT courses like Java, HTML, CSS");
//setting preferable size(height and width) for the label contentlabelContent.setPrefSize(400, 100);
Label labelContent1 = new Label(
"EDUCBA is teach you every concept in simple and clear manner");
labelContent1.setPrefSize(400, 100);
Label labelContent2 = new Label(
"EDUCBA is offer you big discount for every course");
labelContent2.setPrefSize(400, 100);
//creating VBox for adding all labels
VBox vBox=new VBox();
vBox.getChildren().add(labelContent);
vBox.getChildren().add(labelContent1);
vBox.getChildren().add(labelContent2);
// Setting the content to the ScrollPane
scrollPane.setContent(vBox);
// Always show vertical scroll bar for scrolling
scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);
//adding scroll pane to the scene
Scene scene = new Scene(scrollPane, 300, 400);
primaryStage.setScene(scene);
//showing the output
primaryStage.show();
}
public static void main(String[] args) {
//invoking main method from JVM
launch(args);
}
}
Output:
Explanation:
- We have added label content to the VBox and VBox to the ScrollPane.
- We can see in the above output, content is scrolling horizontal direction because of setHPolicy().
Example #3
Adding an image to the ScrollPane and Horizontal and Vertical Scroll Bar Example
JavaFX Code:
package com.scrollpane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class ScrollPaneImageVHScrollBar extends Application {
@Override
public void start(Stage primaryStage) {
// setting the title of application
primaryStage.setTitle("ScrollPane Image Horizontal and Vertical");
// Create a ScrollPane
ScrollPane scrollPane = new ScrollPane();
//creating image for adding image to scroll pane
Image roses = new Image(getClass().getResourceAsStream("2.jpg"));
scrollPane.setContent(new ImageView(roses));
// Always show horizontal scroll bar for scrolling
scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);
// Always show vertical scroll bar for scrolling
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
//adding scroll pane to the scene
Scene scene = new Scene(scrollPane, 200, 100);
primaryStage.setScene(scene);
//showing the output
primaryStage.show();
}
public static void main(String[] args) {
//invoking main method from JVM
launch(args);
}
}
Output:
Explanation:
- We have added the image to the ScrollPane.
- We can see in the above output, image is scrolling vertical direction and horizontal direction because of setVPolicy() and setHPolicy() respectively.
Conclusion
JavaFX ScrollPane is used to make the content of elements become scrollable by applying scroll bar policies setVPolicy() and setHPolicy().
Recommended Articles
This is a guide to Javafx Scrollpane. Here we discuss How to Create ScrollPane in JavaFX along with the examples and outputs. You may also have a look at the following articles to learn more –