How to make scrollbar display when resizing ScrollPane? (in JavaFX)

I have a FlowPane inside a ScrollPane. Inside the stream, I put four rectangles. When I changed (reduced) the window, the scroll bar did not show the scroll bar, as shown below:

enter image description here

But he appeared 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) What scrollPane property should I check to get the default value for displaying the scroll bar?

ii) How do I change the code to display the scroll bar at the desired point (when resizing)?

+4
source share
1 answer

ScrollBar ScrollPane.setHbarPolicy(...) ScrollPane.setVbarPolicy(...).

, , scrollPane.setFitToWidth(true) scrollPane.setFitToHeight(true), , ScrollPane FlowPane , . scrollPane.setFitToHeight(true). scrollPane , .

+7

All Articles