Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

javafx - Stackpane - MouseClick to be listened by both its children

Background

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

I have a StackPane, with ScrollPane and AnchorPane as its children, added in the same order.

ScrollPane contains a list of images contained in ImageView's, while the Anchorpane has two buttons, anchored to the left and right corners, to scroll the images !

An image which describes the layout

Problem

The scrolling is working fine, as the buttons are placed on the AnchorPane, which is the top child on the StackPane.

I want to implement the double-click on the imageView to open it in fullscreen. Since the imageView is a child of ScrollPane, it is not able to listen to the mouse event.

Is there a way through which I can make the ImageView listen to the mouseEvent ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...