Thursday, June 23, 2016

68. ScrollBar

We use a ScrollBar to move a VBox layout.


The VBox holds a rectangle that is wider than the window size. We bind the layout x coordinate to the ScrollBar.


package ex68;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Ex68 extends Application {

    @Override
    public void start(Stage stage) {
        
        final double WIDTH = 800;

        ScrollBar hScroll = new ScrollBar();
        hScroll.setLayoutX(10); hScroll.setLayoutY(150);
        hScroll.setMin(0); hScroll.setMax(WIDTH);
        
        LinearGradient lg = new LinearGradient(
            0, 0.5, 1, 0.5, true, CycleMethod.NO_CYCLE, 
                new Stop(0,Color.RED),
                new Stop(0.5, Color.GREEN),
                new Stop(1,Color.BLUE));
        
        Rectangle rect = new Rectangle(10,10,WIDTH,100);
        rect.setFill(lg);
        
        HBox hbox = new HBox(rect);
        hbox.setPadding(new Insets(20));
  
        Group root = new Group(hbox, hScroll);
        
        Scene scene = new Scene(root, 400, 200, Color.LINEN);
        stage.setTitle("Example 68. ScrollBar"); 
        
        hbox.layoutXProperty().bind(hScroll.valueProperty().negate());
       
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

This is the output after scrolling to the right end:


No comments:

Post a Comment