Quick Contact

    Hibernate

    JavafxScrollpane

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

    Examples of JavafxScrollpane

    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(
    "DUCAT is atraining platform for IT courses like Java, PHP, CSS");
    //setting preferable size(height and width) for the label content
    labelContent.setPrefSize(400, 100);
    Label labelContent1 = new Label(
    "DUCAT is teach you every concept in simple and clear manner");
    labelContent1.setPrefSize(400, 100);
    Label labelContent2 = new Label(
    "DUCAT 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);
    }
    }
    

    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(
    "DUCAT is teach you every concept in simple and clear manner");
    labelContent1.setPrefSize(400, 100);
    Label labelContent2 = new Label(
    "DUCAT 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);
    }
    }
    

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


    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.