JavaFx : Disable Divider
Asked Answered
B

4

8

I have a JavaFX application with a SplitPane. I want to disable the Divider on SplitPane, so it would not be possible to change its position when application is running. How can I do this?

Bravo answered 5/11, 2014 at 17:3 Comment(0)
W
9

There's no API for that, so once the scene is shown we have to use a lookup function to find the node by its id. In this case, the Divider has this id: split-pane-divider.

Once we find the node, we set it transparent to mouse events:

@Override
public void start(Stage primaryStage) {
    final SplitPane splitPane = new SplitPane();
    splitPane.setOrientation(Orientation.HORIZONTAL);
    splitPane.setDividerPositions(new double[]{0.5});
    splitPane.getItems().add(new StackPane(new Label("Left")));
    splitPane.getItems().add(new StackPane(new Label("Right")));

    Scene scene = new Scene(splitPane, 300, 250);

    primaryStage.setScene(scene);
    primaryStage.show();

    splitPane.lookupAll(".split-pane-divider").stream()
            .forEach(div ->  div.setMouseTransparent(true) );

}
Wootan answered 5/11, 2014 at 17:14 Comment(5)
This is great, but for applications with more than one split pane, all will be disabled. How to set the id of a split panes divider?Shaughn
You can use the lookup only with the intended splitPane that contains the divider you want to disable.Manciple
Does this still work with JavaFX 8? I can't get it to work. No node is found with lookup.Padua
It still works, indeed. You can use ScenicView to find the divider, and see that it still uses the same style class. Also make sure to call lookup after the stage is shown.Manciple
This didn't work for me. I added a listener to the divider and rewrote the slider position when it was changed. Its hacky but only a few lines of code and worked.Oyler
H
5

None of the above posts worked for me. I have found this solution which worked for me: This code works for the case when your splitPane is divided in the middle and has only one divider, therefore the divider's position is set to 0.5. If you don't know the position of the divider, you can get it by divider.getPosition();.

        Divider divider = splitPane.getDividers().get(0);
    divider.positionProperty().addListener(new ChangeListener<Number>()      
    {             
        @Override 
        public void changed( ObservableValue<? extends Number> observable, Number oldvalue, Number newvalue )
        {
            divider.setPosition(0.5);
        }
    }); 

This code is in the initialize() method of the GUI Controller class.

Harrold answered 24/6, 2018 at 17:46 Comment(1)
This solution works perfectly when you still need the mouse NOT to be transparent.Uniformed
A
1

Set mouseTransparent="true" of SplitPane in Fxml file.

<SplitPane dividerPositions="0.5" mouseTransparent="true" prefHeight="652.0" prefWidth="858.0">
Aweless answered 10/11, 2016 at 14:53 Comment(1)
This results in all containing nodes in the splitpane also beeing mouseTransparent. Thisway you can't interact with any child via mouse anymore.Padua
T
0

You can also modify the Skin class for SplitPane. Just copy the code from GrepCode for SplitPaneSkin (available here) and remove the MouseListeners in method initializeDivderEventHandlers() and also the setCursor calls in method setGrabberStyle() and then you can't resize the pane by dragging the divider ;-) At the end you only have to set the skin to the SplitPane by calling setSkin.

Trioxide answered 16/6, 2015 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.