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.
source share