Trending September 2023 # Learn How To Create Scrollpane In Javafx? # Suggested October 2023 # Top 18 Popular | Lanphuongmhbrtower.com

Trending September 2023 # Learn How To Create Scrollpane In Javafx? # Suggested October 2023 # Top 18 Popular

You are reading the article Learn How To Create Scrollpane In Javafx? updated in September 2023 on the website Lanphuongmhbrtower.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn How To Create Scrollpane In Javafx?

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

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 ();

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

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) { primaryStage.setTitle("ScrollPane Vertical"); ScrollPane scrollPane = new ScrollPane(); Label labelContent = new Label( "EDUCBA is online teaching platform for IT courses like Java, HTML, CSS"); 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); VBox vBox=new VBox(); vBox.getChildren().add(labelContent); vBox.getChildren().add(labelContent1); vBox.getChildren().add(labelContent2); scrollPane.setContent(vBox); scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS); Scene scene = new Scene(scrollPane, 551, 201); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { 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) { primaryStage.setTitle("ScrollPane Vertical"); ScrollPane scrollPane = new ScrollPane(); Label labelContent = new Label( "EDUCBA is online teaching platform for IT courses like Java, HTML, CSS"); 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); VBox vBox=new VBox(); vBox.getChildren().add(labelContent); vBox.getChildren().add(labelContent1); vBox.getChildren().add(labelContent2); scrollPane.setContent(vBox); scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS); Scene scene = new Scene(scrollPane, 300, 400); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { 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) { primaryStage.setTitle("ScrollPane Image Horizontal and Vertical"); ScrollPane scrollPane = new ScrollPane(); Image roses = new Image(getClass().getResourceAsStream("2.jpg")); scrollPane.setContent(new ImageView(roses)); scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS); scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS); Scene scene = new Scene(scrollPane, 200, 100); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { 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 –

You're reading Learn How To Create Scrollpane In Javafx?

Update the detailed information about Learn How To Create Scrollpane In Javafx? on the Lanphuongmhbrtower.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!