The strange behavior of two mouse events fires endlessly in the hint code

I am trying to implement a custom tooltip using javafx.stage.Popup . Sample code:

 public class PopupDemo extends Application { private Popup tooltip; private final SepiaTone sepiaTone = new SepiaTone(); public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("PopupDemo"); Label content = new Label(); content.setStyle("-fx-background-color:#FCFBBD; -fx-padding: 5; -fx-border-color: #BFBD3B"); tooltip = new Popup(); tooltip.getContent().add(content); VBox vbox = new VBox(10); for (int i = 0; i < 5; i++) { final Label lbl = new Label("item " + i); lbl.setStyle("-fx-border-color:darkgray; -fx-background-color:lightgray"); lbl.setMaxSize(80, 60); lbl.setMinSize(80, 60); lbl.setAlignment(Pos.CENTER); lbl.setOnMouseEntered(new EventHandler<MouseEvent>() { @Override public void handle(final MouseEvent e) { lbl.setEffect(sepiaTone); lbl.setStyle("-fx-cursor: hand"); Label content = (Label) tooltip.getContent().get(0); content.setText(lbl.getText()); tooltip.show(lbl, e.getScreenX(), e.getScreenY()); } }); lbl.setOnMouseExited(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent e) { lbl.setEffect(null); lbl.setStyle("-fx-cursor: default"); tooltip.hide(); } }); vbox.getChildren().add(lbl); } StackPane root = new StackPane(); root.setPadding(new Insets(20)); root.getChildren().add(vbox); primaryStage.setScene(new Scene(root, 600, 400)); primaryStage.show(); } } 

When I move the mouse over the shortcuts, a popup appears and it works great. But in some cases, two mouse event handlers OnMouseEntered and OnMouseExited are called continuously one after another. This can be reproduced by following the example, maximizing the window and hanging labels.

Is there any way to avoid this? I am using JavaFX 2.0.1. Thanks.

+4
source share
2 answers

This is a classic problem: you hover over a point, node gets MouseEntered - a tooltip appears under the mouse and encompasses the MouseExited trigger MouseExited .

To avoid this, you can change the call to tooltip.show(lbl, e.getScreenX(), e.getScreenY()) to

 tooltip.show(lbl, e.getScreenX() + 1, e.getScreenY() + 1); 
+6
source

Actually, this is not such an answer as indicating that you can try or continue.

You can look at the Tooltip Skin implementation and Behavior to see how it handles some of these cases.

The easiest way to implement a custom popup is to simply use the popup hint as you need with css and use the Tooltip setGraphic method to add any custom Node content you want.

If you prefer to use your own implementation, I think you need to keep track of whether the popup was displayed or not, so you are not trying to show it if it is already displayed, etc. You may also need to cause the pop-up to be hidden by having the mouse exit handler installed in the pop-up. You may also need to click to reject the function for the popup by embedding a mouse click handler in the popup. You should also consider whether to perform a direct subclass of Popup or PopupControl , although the use of the popup is, in your opinion, more straightforward.

0
source

Source: https://habr.com/ru/post/1395242/


All Articles