JavaFX: Add children to ScrollPane

Alex picture Alex · Jul 24, 2016 · Viewed 20k times · Source

I have a Pane Object where users can drag and drop various ImageViews. For this, I used pane.getChildren(imageViewObject) method

Now, after replacing Pane with ScrollPane, it does not have this method. So I don't know how to get arrount this issue.

Thank you in advance

Answer

Kachna picture Kachna · Jul 24, 2016

you can specify only one node with ScrollPane. To create a scroll view with more than one component, use layout containers or the Group class.

Pane pane = ...;
ScrollPane sp = new ScrollPane();
sp.setContent(pane);

Example:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author kachna
 */
public class Test extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        VBox root = new VBox();
        root.getChildren().addAll(new Button("button1"), new Button("button2"), new Button("button3"));
        root.setSpacing(10);
        root.setPadding(new Insets(10));
        ScrollPane sp = new ScrollPane();
        sp.setContent(root);
        sp.setPannable(true); // it means that the user should be able to pan the viewport by using the mouse.
        Scene scene = new Scene(sp, 100, 100);
        stage.setScene(scene);
        stage.show();
    }

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

}