- JavaFX Tutorial
- JavaFX - Home
- JavaFX - Overview
- JavaFX Installation and Architecture
- JavaFX - Installation Using Netbeans
- JavaFX - Installation Using Eclipse
- JavaFX - Installation using Visual Studio Code
- JavaFX - Architecture
- JavaFX - Application
- JavaFX 2D Shapes
- JavaFX - 2D Shapes
- JavaFX - Drawing a Line
- JavaFX - Drawing a Rectangle
- JavaFX - Drawing a Rounded Rectangle
- JavaFX - Drawing a Circle
- JavaFX - Drawing an Ellipse
- JavaFX - Drawing a Polygon
- JavaFX - Drawing a Polyline
- JavaFX - Drawing a Cubic Curve
- JavaFX - Drawing a Quad Curve
- JavaFX - Drawing an Arc
- JavaFX - Drawing an SVGPath
- JavaFX Properties of 2D Objects
- JavaFX - Stroke Type Property
- JavaFX - Stroke Width Property
- JavaFX - Stroke Fill Property
- JavaFX - Stroke Property
- JavaFX - Stroke Line Join Property
- JavaFX - Stroke Miter Limit Property
- JavaFX - Stroke Line Cap Property
- JavaFX - Smooth Property
- Operations on 2D Objects
- JavaFX - 2D Shapes Operations
- JavaFX - Union Operation
- JavaFX - Intersection Operation
- JavaFX - Subtraction Operation
- JavaFX Color and Texture
- JavaFX - Colors
- JavaFX Text
- JavaFX - Text
- JavaFX Effects
- JavaFX - Effects
- JavaFX Transformations
- JavaFX - Transformations
- JavaFX Animations
- JavaFX - Animations
- JavaFX Images
- JavaFX - Images
- JavaFX 3D Shapes
- JavaFX - 3D Shapes
- JavaFX - Creating a Box
- JavaFX - Creating a Cylinder
- JavaFX - Creating a Sphere
- Properties of 3D Objects
- JavaFX - Cull Face Property
- JavaFX - Drawing Modes Property
- JavaFX - Material Property
- JavaFX Event Handling
- JavaFX - Event Handling
- JavaFX - Using Convenience Methods
- JavaFX - Event Filters
- JavaFX - Event Handlers
- JavaFX UI Controls
- JavaFX - UI Controls
- JavaFX - ListView
- JavaFX - Accordion
- JavaFX - ButtonBar
- JavaFX - ChoiceBox
- JavaFX - HTMLEditor
- JavaFX - MenuBar
- JavaFX - Pagination
- JavaFX - ProgressIndicator
- JavaFX - ScrollPane
- JavaFX - Separator
- JavaFX - Slider
- JavaFX - Spinner
- JavaFX - SplitPane
- JavaFX - TableView
- JavaFX - TabPane
- JavaFX - ToolBar
- JavaFX - TreeView
- JavaFX - Label
- JavaFX - CheckBox
- JavaFX - RadioButton
- JavaFX - TextField
- JavaFX - PasswordField
- JavaFX - FileChooser
- JavaFX - Hyperlink
- JavaFX - Tooltip
- JavaFX - Alert
- JavaFX - DatePicker
- JavaFX - TextArea
- JavaFX Charts
- JavaFX - Charts
- JavaFX - Creating Pie Chart
- JavaFX - Creating Line Chart
- JavaFX - Creating Area Chart
- JavaFX - Creating Bar Chart
- JavaFX - Creating Bubble Chart
- JavaFX - Creating Scatter Chart
- JavaFX - Creating Stacked Area Chart
- JavaFX - Creating Stacked Bar Chart
- JavaFX Layout Panes
- JavaFX - Layout Panes
- JavaFX - HBox Layout
- JavaFX - VBox Layout
- JavaFX - BorderPane Layout
- JavaFX - StackPane Layout
- JavaFX - TextFlow Layout
- JavaFX - AnchorPane Layout
- JavaFX - TilePane Layout
- JavaFX - GridPane Layout
- JavaFX - FlowPane Layout
- JavaFX CSS
- JavaFX - CSS
- Media with JavaFX
- JavaFX - Playing Video
- JavaFX Useful Resources
- JavaFX - Quick Guide
- JavaFX - Useful Resources
- JavaFX - Discussion
JavaFX - ScrollPane
ScrollPane is a control that provides a scrollable viewport of its contents. It allows the user to scroll the content vertically or horizontally by using scroll bars. It is used to display a component that is large or one whose size can change dynamically when the screen viewport is limited. Remember, the size of the scroll bars depends on the size of the component. The below figure shows a scrollable viewport with vertical scroll bars −
ScrollPane in JavaFX
In JavaFX, the scrollpane control is represented by a class named ScrollPane. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a ScrollPane control in JavaFX. This class has the following constructors −
ScrollPane() − It constructs a ScrollPane without any node.
ScrollPane(Node content) − It constructs a new ScrollPane with the specified node.
Steps to create a ScrollPane in JavaFX
To create a ScrollPane in JavaFX, follow the steps given below.
Step 1: Create a node to display within the ScrollPane
In JavaFX, the scrollpane can display nodes that can contain an image, a text or a chart. Hence, instantiate the respected class to create the desired node. Here, we are using an image as a content for the ScrollPane −
// create an image view ImageView imageTp = new ImageView(new Image("tutorials_point.jpg"));
Step 2: Instantiate the ScrollPane class
Instantiate the class named ScrollPane inside the start() method. This action will create a ScrollPane for the ImageView.
// create a scroll pane ScrollPane newscroll = new ScrollPane();
Step 3: Set the Content of the ScrollPane
To set the content of the ScrollPane, we use the method named setContent(). Pass the ImageView object to this method as a parameter value.
// set the content of the scroll pane newscroll.setContent(imageTp);
Step 4: Launch the Application
Once the ScrollPane is created and its content is set, follow the given steps below to launch the application properly −
Firstly, instantiate the class named Scene by passing the ScrollPane object as a parameter value to its constructor along with the dimensions of the application screen.
Then, set the title of the stage using the setTitle() method of the Stage class.
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
Display the contents of the scene using the method named show().
Lastly, the application is launched with the help of the launch() method.
Example
The following JavaFX program demonstrates how to create a ScrollPane within JavaFX application. Save this code in a file with the name JavafxScrollpane.java.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.stage.Stage; public class JavafxScrollpane extends Application { @Override public void start(Stage stage) { // creating an image view ImageView imageTp = new ImageView(new Image("tutorials_point.jpg")); // creating a scroll pane ScrollPane newscroll = new ScrollPane(); // setting the content of the scroll pane newscroll.setContent(imageTp); // creating a scene and stage Scene scene = new Scene(newscroll, 500, 300); stage.setTitle("ScrollPane in JavaFX"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxScrollpane.java java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxScrollpane
Output
When we execute the above code, it will generate the following output.
Disable Vertical Scroll bar of ScrollPane in JavaFX
The ScrollPane class provides two methods namely setHbarPolicy() and setVbarPolicy() to specify when to use the scroll bars. To enable the scroll bar, we pass the ScrollBarPolicy.ALWAYS property to the respective methods and to disable, we use ScrollBarPolicy.NEVER property.
Example
In the following example, we are going to disable the vertical scroll bar of the ScrollPane. Save this code in a file with the name JavafxScrollpane.java.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.control.ScrollPane.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.stage.Stage; public class JavafxScrollpane extends Application { @Override public void start(Stage stage) { // creating an image view ImageView imageTp = new ImageView(new Image("tutorials_point.jpg")); // creating a scroll pane ScrollPane newscroll = new ScrollPane(); // disbaling the vertical scroll bar newscroll.setHbarPolicy(ScrollBarPolicy.ALWAYS); newscroll.setVbarPolicy(ScrollBarPolicy.NEVER); // setting the content of the scroll pane newscroll.setContent(imageTp); // creating a scene and stage Scene scene = new Scene(newscroll, 500, 300); stage.setTitle("ScrollPane in JavaFX"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
Compile and execute the saved Java file from the command prompt by using the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxScrollpane.java java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxScrollpane
Output
On executing the above code, it will generate the following output.