0

I have a problem with the SplitPane inside a ScrollPane. I need an extendable scrollPane similar to the TableView so when I have too many items I have scrollbar, when all of my items are visible then hide the scrollbar.

ScrollPane has a switch : fitToHeight but both true/false doesn't do the job I need. I will upload two screenshots to show what I mean. If it is set to true then I simply don't have the scroll which is not a solution because I may have too much data to fit in one window so I need the scroll. If it is set to false then I always have scrollbar even if there is no data to show at the bottom of the view.

I would like to be the scrollbar dynamic, so when I drag the last divider and there is no space to fit the last item, the scrollbar should appear, when there is space for every item in the SplitPane then it should not be displayed.

Any idea how can it be achieved?

The relevant code:

Fxml:

<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.VBox?>
<ScrollPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fitToWidth="true"
            fx:controller="test.Controller">
    <SplitPane fx:id="splitPane" orientation="VERTICAL">
        <VBox spacing="5" minHeight="20">
            <Label text="*Title*" VBox.vgrow="NEVER"/>
            <TextArea VBox.vgrow="ALWAYS"/>
        </VBox>
        <VBox minHeight="20" spacing="5">
            <Label text="*Title*" VBox.vgrow="NEVER"/>
            <TableView VBox.vgrow="ALWAYS">
                <columns>
                    <TableColumn/>
                </columns>
            </TableView>
        </VBox>
        <VBox minHeight="20" spacing="5">
            <Label text="*Title*" VBox.vgrow="NEVER"/>
            <TableView VBox.vgrow="ALWAYS">
                <columns>
                    <TableColumn/>
                </columns>
            </TableView>
        </VBox>
        <VBox minHeight="20" spacing="5">
            <Label text="*Title*" VBox.vgrow="NEVER"/>
            <TextArea VBox.vgrow="NEVER"/>
        </VBox>
        <padding>
            <Insets topRightBottomLeft="5"/>
        </padding>
    </SplitPane>
</ScrollPane>

Controller:

public class Controller implements Initializable {

    @FXML
    private SplitPane splitPane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        splitPane.setDividerPositions(0d,.33d, .66d,1d);
    }

}

Main:

public class Main extends Application {

    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("View.fxml"));
        ScrollPane pane = loader.load();
        primaryStage.setScene(new Scene(pane, 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Here I have the scrollbar even if there is no need to scroll, every item is visible

Fit off

Here I don't have the scrollbar

Fit on

Sunflame
  • 2,993
  • 4
  • 24
  • 48
  • on face value, it looks like a misconception of "empty space": the pref/height of the splitpane is still the same - doesn't matter if the sum of the really visible is less. So the scrollPane has no reason to adjust its scrollbar. To see that: keep only the first two and see how the scrollbar appears/disappears on resizing the window (if its fitToHeight is false). Don't know how to solve (could even be a bug in any of the layouts), my direction would be to find a way to make the splitpane recalc its pref. – kleopatra Apr 19 '20 at 11:17
  • @kleopatra true, it is not "empty", it is the last item in my case the `VBox` I am trying to recalc the scrollPane's or splitPane's height, but something fails every time. I keep trying, I asked it if someone had some similar problem, or knows the solution to help me :) – Sunflame Apr 20 '20 at 07:12
  • how are you trying to calculate the height and how is it failing? – b3tuning Apr 25 '20 at 02:37
  • What if you set the `minHeight` property of the `SplitPane` to `Region.USE_PREF_SIZE`? – Slaw Apr 26 '20 at 09:31
  • @Slaw it doesn't work. Almost same as before, there is the scrollbar but if I try to drag the last divider it hides simply the last item in the splitpane and doesn't extend the pane to scroll down. – Sunflame Apr 27 '20 at 07:09
  • @b3tuning I tried to bruteforce it, so adding a listener to the last item's height and recalculate the height of the splitpane when the last item's height changes. It worked in a way but it caused other glitches. – Sunflame Apr 27 '20 at 07:11

0 Answers0