import com.sun.javafx.runtime.VersionInfo; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Orientation; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.FlowPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class ScrollPaneApp extends Application { private ScrollPane scrollPane; private Slider prefViewPortWidth; private Slider prefViewPortHeight; public static void main(String[] args) { launch(args); } HBox root; @Override public void start(Stage stage) throws Exception { stage.setTitle(VersionInfo.getRuntimeVersion()); stage.setScene(createScene()); stage.show(); new Thread(new Runnable() { @Override public void run() { try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException ex) { Logger.getLogger(ScrollPaneApp.class.getName()).log(Level.SEVERE, null, ex); } Platform.runLater(new Runnable() { @Override public void run() { ScrollPane _pane = new ScrollPane(); HBox hbox = new HBox(30); VBox vbox1 = new VBox(10); vbox1.getChildren().addAll(new Label("one"), new Button("two"), new CheckBox("three"), new RadioButton("four"), new Label("five")); VBox vbox2 = new VBox(10); vbox2.getChildren().addAll(new Label("one"), new Button("two"), new CheckBox("three"), new RadioButton("four"), new Label("five")); hbox.getChildren().addAll(vbox1, vbox2); _pane.setContent(hbox); _pane.setFocusTraversable(false); _pane.setTranslateX(50); _pane.setTranslateY(50); Pane pane = new Pane(); pane.getChildren().add(_pane); root.getChildren().add(pane); pane.setPrefSize(600, 600); } }); } }).start(); } Scene createScene() { root = new HBox(); //--------------------------------- ScrollPane _pane = new ScrollPane(); HBox hbox = new HBox(30); VBox vbox1 = new VBox(10); vbox1.getChildren().addAll(new Label("one"), new Button("two"), new CheckBox("three"), new RadioButton("four"), new Label("five")); VBox vbox2 = new VBox(10); vbox2.getChildren().addAll(new Label("one"), new Button("two"), new CheckBox("three"), new RadioButton("four"), new Label("five")); hbox.getChildren().addAll(vbox1, vbox2); _pane.setContent(hbox); _pane.setFocusTraversable(false); _pane.setTranslateX(50); _pane.setTranslateY(50); scrollPane = _pane; //--------------------------------- Button content = new Button("Change content"); content.setOnAction(new EventHandler() { @Override public void handle(ActionEvent actionEvent) { Pane canvas = new Pane(); canvas.setStyle("-fx-background-color: blue;"); canvas.setPrefHeight(100); canvas.setPrefWidth(100); scrollPane.setContent(canvas); } }); Pane pane = new Pane(); pane.setPrefSize(600, 600); pane.getChildren().add(scrollPane); root.getChildren().add(pane); return new Scene(root, 1200, 800); } static Group createCustomContent(int height, int width) { Group res = new Group(); Rectangle r = new Rectangle(); r.setStroke(Color.BLACK); r.setStyle("-fx-border-color: GREEN;"); res.getChildren().add(r); for (int i = 10; i < height; i += 10) { Line line1 = new Line(0, i, i - 5, i); Line line2 = new Line(i, 0, i, i - 5); Line line3 = new Line(i - 5, i, i - 5, height); Line line4 = new Line(i, i - 5, width, i - 5); line1.setStroke(Color.RED); line2.setStroke(Color.YELLOW); line3.setStroke(Color.BLUE); line4.setStroke(Color.MAGENTA); res.getChildren().addAll(line1, line2, line3, line4); } Rectangle rec = new Rectangle(0, 0, width, height); rec.setFill(Color.TRANSPARENT); rec.setStroke(Color.RED); res.getChildren().add(rec); return res; } }