JavaFX ScrollPane

0

JavaFX ScrollPane tutorial:

  • JavaFX ScrollPane is used to show a big content in short space.
  • So it show a portion of content at a time.
  • If scroll bar needed it added automatically.

Constructors of scroll pane:

  • ScrollPane()
    Creates a new ScrollPane.
  • ScrollPane(Node content)
    Creates a new ScrollPane with addition of Node.

Methods used in scroll pane:

  • setContent(Node value)
    Set content node to the node for which we apply.
    Ex –> a.setContent(Node hb) it add node to the “a”.

Scroll Bar Controls(Policy):

We can control the appearance of Scroll bars:

Two methods setHbarPolicy() and setVbarPolicy() are used to show bar or not AS :

  • Always
  • Never
  • When Necessary

For example :
scrollpane.setHbarPolicy(
ScrollBarPolicy.ALWAYS);

  • It set the scrollbar for horizontal permanently.
  • Example of scroll pane:

    
    package javafxtuts;
    
    import javafx.application.Application;
    import javafx.scene.Scene;;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.control.ScrollPane.ScrollBarPolicy;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    /**
     *
     * @author javafxtuts
     */
    public class Javafxtuts extends Application {
    
      @Override
      public void start(Stage primaryStage) {
          
          // setting title of application
          primaryStage.setTitle("javafxtuts.com");
          
          //to create a ScrollPane sp
          ScrollPane sp =new ScrollPane();
          
          //To create a rectangle shape
          Rectangle rect = new Rectangle(
          				400,800,Color.GREEN);
          sp.setContent(rect);
         
          sp.setHbarPolicy(
          			ScrollBarPolicy.ALWAYS);
          sp.setVbarPolicy(
          			ScrollBarPolicy.NEVER);
          
    
          //to create a scane using sp
          Scene scene = new Scene(sp, 350, 175);
          primaryStage.setScene(scene);
          primaryStage.show();
      }
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    OutPut :

    Explanation of scroll pane example:

      • Create a ScrollPane AS
        ScrollPane sp =new ScrollPane();
      • Rectangle rect = new Rectangle(400, 800, Color.GREEN);
        It create a rectangle of size 400×800.
      • sp.setContent(rect);
        It set rectangle as a child(content)of sp.
      • sp.setHbarPolicy(ScrollBarPolicy.ALWAYS);
        It set Horizontal bar permanently fix .
    • sp.setVbarPolicy(ScrollBarPolicy.NEVER);
      It disable the vertical Bar.
    Share.

    Leave A Reply