5

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:

enter image description here

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)?

1
  • Which JDK Version are you using? Jan 6, 2014 at 14:13

1 Answer 1

8

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.

1
  • Thanks James, commenting scrollPane.setFitToHeight(true)works.
    – Angom
    Jan 7, 2014 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.