I have a FlowPane inside a ScrollPane. Inside the flow pane I put four Rectangle shapes. When I resized (reducing) the window the scroll pane didn't showed up the scrollbar as shown below:
But it showed up at some point when I reduce the size more.
Below is my code:
public class FlowPaneInsideScrollPane extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
ScrollPane scrollPane = new ScrollPane();
scrollPane.setFitToWidth(true);
scrollPane.setFitToHeight(true);
FlowPane flowPane = new FlowPane();
flowPane.setStyle("-fx-background-color: blue");
flowPane.setPadding(new Insets(10, 10, 10, 10));
flowPane.setHgap(10);
flowPane.setVgap(10);
Rectangle rect1 = new Rectangle(100, 100, Color.RED);
Rectangle rect2 = new Rectangle(100, 100, Color.RED);
Rectangle rect3 = new Rectangle(100, 100, Color.RED);
Rectangle rect4 = new Rectangle(100, 100, Color.RED);
flowPane.getChildren().add(rect1);
flowPane.getChildren().add(rect2);
flowPane.getChildren().add(rect3);
flowPane.getChildren().add(rect4);
scrollPane.setContent(flowPane);
primaryStage.setScene(new Scene(scrollPane, 500, 500));
primaryStage.show();
}
}
i) Which property of scrollPane should I check to get the default value for displaying scrollbar?
ii) How will modify the code so as to display scrollbar at a desired point (while resizing)?
ScrollBar policies are determined by ScrollPane.setHbarPolicy(...) and ScrollPane.setVbarPolicy(...).
I think the problem here is that you have scrollPane.setFitToWidth(true)
and scrollPane.setFitToHeight(true)
, which is causing the ScrollPane to try to force the FlowPane to be the same size as the scroll pane's viewport. Try commenting out scrollPane.setFitToHeight(true). You can set the background color of the scrollPane to the same background color of the flow pane if you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With