”;
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.
”;