简体   繁体   中英

Is it possible to detect when the mouse cursor is over a hidden stage or scene in JavaFX?

I'm trying to implement a control that exists on a stage that, when moused over will be shown, and then when the mouse leaves it's bounds, hides itself.

I've tried

Stage.getScene().setOnMouseEntered((MouseEvent mEntered) -> {Stage.show();});
Stage.getScene().setOnMouseExited((MouseEvent mExited) -> {Stage.hide();});

I'm not really very surprised it didn't work, but it doesn't help me very much.

Is it possible to detect when the mouse is over the Stage or Scene when it is hidden?

I've read your multiple threads now and didn't get what you really intend to achieve when you put it all together. Maybe if you elaborate on that, another solution may be more appropriate.

Anyway, for your transparent buttons the solution is to use a transparency low enough to let the content below it shine through, but still not totally transparent in order to register mouse events. You wouldn't get mouse events on the application if it were fully transparent.

Here's an example about how you could do it:

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {            


            Pane pane = new Pane();

            HBox hbox = new HBox();

            hbox.setSpacing(12);

            Button button1 = new Button("Button 1");
            Button button2 = new Button("OMG Magic!");
            Button button3 = new Button("Button 3");

            hbox.getChildren().addAll(button1, button2, button3);

            pane.getChildren().add( hbox);


            final Scene scene = new Scene(pane, Color.TRANSPARENT);

//          scene.getRoot().setStyle("-fx-background-color: transparent");
            scene.getRoot().setStyle("-fx-background-color: rgba(1,1,1,0.004)");

            primaryStage.initStyle(StageStyle.TRANSPARENT);
            primaryStage.setScene(scene);

            primaryStage.show();

            // initial opacity of button 2
            button2.setOpacity(0.0);

            // button2: fade-in on mouse scene enter
            scene.addEventHandler(MouseEvent.MOUSE_ENTERED, evt ->  {

                System.out.println("Mouse entered");

                FadeTransition fadeTransition = new FadeTransition(Duration.millis(300), button2);
                fadeTransition.setFromValue(0.0);
                fadeTransition.setToValue(1.0);
                fadeTransition.play();

            });

            // button2: fade-out on mouse scene exit
            scene.addEventHandler(MouseEvent.MOUSE_EXITED, evt ->  {

                System.out.println("Mouse exited");

                FadeTransition fadeTransition = new FadeTransition(Duration.millis(300), button2);
                fadeTransition.setFromValue(1.0);
                fadeTransition.setToValue(0.0);
                fadeTransition.play();              
            });   


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

It shows 3 buttons. When you enter the button 2 or the pane between buttons 1 and button 3 with the mouse, then button 2 will magically appear :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM