package graphics.api.control.scrollview; import javafx.beans.property.*; import javafx.application.Application; import javafx.stage.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.beans.property.IntegerProperty; import javafx.beans.binding.Bindings; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.event.EventHandler; public class ScrollViewDynamicPolicyChange extends Application { public static void main(String[] args) { ScrollViewDynamicPolicyChange.launch(args); } IntegerProperty width = new SimpleIntegerProperty(25); IntegerProperty height = new SimpleIntegerProperty(10); ScrollPane scrollView = new ScrollPane(); Button button; Button button2; Button button3; Button button4; Button button5; Scene scene; @Override public void start(Stage stage) throws Exception { double h = 400; double w = 400; scrollView.setManaged(true); scrollView.setPrefWidth(56); scrollView.setPrefHeight(23); button = new Button("Inside"); button.prefWidthProperty().bind(width); button.prefHeightProperty().bind(height); scrollView.setContent(button); scrollView.setTranslateX(90); scrollView.setTranslateY(20); scene = new Scene(new Group(), w, h); button2 = new Button("Width " + width); button2.textProperty().bind(Bindings.concat("Width ", width)); button2.setTranslateX(80); button2.setTranslateY(100); button2.setOnAction(new EventHandler() { public void handle(ActionEvent t) { width.set(width.get() + 5); if (width.get() == 65) { width.set(25); } } }); button3 = new Button("Height " + height); button3.textProperty().bind(Bindings.concat("Height ", height)); button3.setTranslateX(80); button3.setTranslateY(130); button3.setOnAction(new EventHandler() { public void handle(ActionEvent t) { height.set(height.get() + 2); if (height.get() == 38) { height.set(10); } } }); button4 = new Button("hbar: " + scrollView.getHbarPolicy()); button4.textProperty().bind(Bindings.concat("hbar: ", scrollView.hbarPolicyProperty())); button4.setTranslateX(80); button4.setTranslateY(160); button4.setOnAction(new EventHandler() { public void handle(ActionEvent t) { if (scrollView.getHbarPolicy().equals(ScrollPane.ScrollBarPolicy.AS_NEEDED)) { scrollView.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); } else if (scrollView.getHbarPolicy().equals(ScrollPane.ScrollBarPolicy.NEVER)) { scrollView.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); } else if (scrollView.getHbarPolicy().equals(ScrollPane.ScrollBarPolicy.ALWAYS)) { scrollView.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); } //The following 2 lines is ugly way to force and make Scrollbar follow the policy. scrollView.setContent(null); scrollView.setContent(button); } }); button5 = new Button("vbar: " + scrollView.getVbarPolicy().name()); System.out.println(scrollView.getVbarPolicy().name()); button5.textProperty().bind(Bindings.concat("vbar: ", scrollView.vbarPolicyProperty())); button5.setTranslateX(80); button5.setTranslateY(190); button5.setOnAction(new EventHandler() { public void handle(ActionEvent t) { if (scrollView.getVbarPolicy().equals(ScrollPane.ScrollBarPolicy.AS_NEEDED)) { scrollView.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); } else if (scrollView.getVbarPolicy().equals(ScrollPane.ScrollBarPolicy.NEVER)) { scrollView.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); } else if (scrollView.getVbarPolicy().equals(ScrollPane.ScrollBarPolicy.ALWAYS)) { scrollView.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); } //The following 2 lines is ugly way to force and make Scrollbar follow the policy. scrollView.setContent(null); scrollView.setContent(button); } }); ((Group) scene.getRoot()).getChildren().clear(); ((Group) scene.getRoot()).getChildren().addAll(FXCollections.observableArrayList(scrollView, button2, button3, button4, button5)); stage.setScene(scene); stage.show(); scrollView.requestFocus(); } }