Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make scrollbar display when a ScrollPane is resized? (in JavaFX)

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

like image 531
Angom Avatar asked Jan 06 '14 13:01

Angom


1 Answers

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.

like image 96
James_D Avatar answered Nov 01 '22 00:11

James_D