Reputation: 3119
I'm trying to add a mouse click listener to a split pane's divider.
It works, but if there is a split pane nested in this split pane, instead of getting this split pane's divider, I get the child splitpane's divider. Here is a working example.
As you can see, the intent here is to put the click listener on the primarySplitPane's divider. However, the listener is put on the secondarySplitPane's divider.
public class DividerTest extends Application {
public static void main ( String [] args ) {
launch ( args );
}
@Override
public void start ( Stage stage ) {
VBox boxA = new VBox();
VBox boxB = new VBox();
VBox boxC = new VBox();
SplitPane secondarySplitPane = new SplitPane();
secondarySplitPane.getItems().addAll( boxB, boxC );
secondarySplitPane.setOrientation( Orientation.VERTICAL );
SplitPane primarySplitPane = new SplitPane();
primarySplitPane.getItems().addAll( boxA, secondarySplitPane );
primarySplitPane.prefWidthProperty().bind( stage.widthProperty() );
primarySplitPane.prefHeightProperty().bind( stage.heightProperty() );
Scene scene = new Scene( new Group(), 800, 600 );
((Group) scene.getRoot()).getChildren().addAll( primarySplitPane );
stage.setScene( scene );
stage.show();
Node primaryDivider = primarySplitPane.lookup(".split-pane-divider");
primaryDivider.setOnMouseClicked( ( e ) -> System.out.println ( "Clicked" ) );
}
}
How can I tell .lookup to get the correct divider?
Upvotes: 2
Views: 654
Reputation: 21799
Your code was working on the vertical divider, but not on the horizontal one. This can be simply fixed by collecting all the dividers, not only the first one. You can check the parent if you want to define different actions on different dividers:
Set<Node> dividers = primarySplitPane.lookupAll(".split-pane-divider");
for (Node divider : dividers) {
if (divider.getParent() == primarySplitPane)
divider.setOnMouseClicked(e -> System.out.println("Primary Clicked" ));
else if (divider.getParent() == secondarySplitPane)
divider.setOnMouseClicked(e -> System.out.println("Secondary Clicked"));
}
Upvotes: 3