Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize JavaFX ScrollPane content to fit current size

I have a BorderPane with a ScrollPane as the center element. It resizes automatically to fill the screen. Inside the ScrollPane is a HBox that has no content but is a drag-and-drop target. Therefore I want it to fill its parent ScrollPane.

What is the preferred way to do this?

What I have tried so far is overriding the ScrollPane.resize(...) method to resize the HBox but it seems rather complicated and unorthodox.

edit: To add some useful code to this, this is how I can workaround the problem, but I believe there has to be some better way to do this:

@Override
public void resize(double width, double height) {
    super.resize(width, height);

    double scrollBarHeight = 2;
    double scrollBarWidth = 2;

    for (final Node node : lookupAll(".scroll-bar")) {
        if (node instanceof ScrollBar) {
            ScrollBar sb = (ScrollBar) node;
            if (sb.getOrientation() == Orientation.HORIZONTAL) {
                System.out.println("horizontal scrollbar visible = " + sb.isVisible());
                System.out.println("width = " + sb.getWidth());
                System.out.println("height = " + sb.getHeight());
                if(sb.isVisible()){
                    scrollBarHeight = sb.getHeight();
                }
            }
            if (sb.getOrientation() == Orientation.VERTICAL) {
                System.out.println("vertical scrollbar visible = " + sb.isVisible());
                System.out.println("width = " + sb.getWidth());
                System.out.println("height = " + sb.getHeight());
                if(sb.isVisible()){
                    scrollBarWidth = sb.getWidth();
                }
            }
        }
    }

    hBox.setPrefSize(width-scrollBarWidth, height-scrollBarHeight);
}

Code partly taken from here: How to access the Scrollbars of a ScrollPane

like image 735
Lennert Avatar asked Jul 10 '13 10:07

Lennert


People also ask

What is the purpose of ScrollPane?

A JScrollPane provides a scrollable view of a component. When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically. Other containers used to save screen space include split panes and tabbed panes.

What is Javapx ScrollPane?

The ScrollPane allows the application to set the current, minimum, and maximum values for positioning the contents in the horizontal and vertical directions. These values are mapped proportionally onto the layoutBounds of the contained node.

Is ScrollPane a container?

A ScrollPane is a Container with built-in scrollbars that can be used to scroll its contents. In the current implementation, a ScrollPane can hold only one Component and has no layout manager. The component within a ScrollPane is always given its preferred size.


2 Answers

Try

ScrollPane scrollPane = new ScrollPane();
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);

without overriding the resize() method.

like image 150
Uluk Biy Avatar answered Nov 29 '22 10:11

Uluk Biy


Setting the fitToHeight or fitToWidth attribute through XML or CSS:

FXML:

<ScrollPane fx:id="myScrollPane" fitToHeight="true" fitToWidth="true" />

CSS:

.my-scroll-pane {
    -fx-fit-to-height: true;
    -fx-fit-to-width: true;
}
like image 27
NDY Avatar answered Nov 29 '22 09:11

NDY