Stackpane - MouseClick to be listened by both its children

后端 未结 1 546
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 01:41

Background

I am trying to build a image gallery based on the description here.

I have a StackPane, with ScrollPane

1条回答
  •  醉梦人生
    2020-12-22 02:17

    Instead of placing the AnchorPane and ScrollPane in a StackPane, try using the AnchorPane as the root of this structure. Add the ScrollPane to it first with appropriate anchors and then add the buttons. That way the buttons will still be on top, but there will be nothing interfering with the mouse events on the content of the scroll pane.

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(...);
    
    
    // scrollPane fills entire anchor pane:
    
    AnchorPane.setLeftAnchor(scrollPane, 0.0);
    AnchorPane.setRightAnchor(scrollPane, 0.0);
    AnchorPane.setTopAnchor(scrollPane, 0.0);
    AnchorPane.setBottomAnchor(scrollPane, 0.0);
    
    Button button1 = new Button(...);
    Button button2 = new Button(...);
    
    // button1 in top left:
    AnchorPane.setTopAnchor(button1, 0.0);
    AnchorPane.setLeftAnchor(button1, 0.0);
    
    // button2 in top right:
    AnchorPane.setTopAnchor(button2, 0.0);
    AnchorPane.setRightAnchor(button2, 0.0);
    
    AnchorPane anchorPane = new AnchorPane();
    anchorPane.getChildren().addAll(scrollPane, button1, button2);
    

    0 讨论(0)
提交回复
热议问题