Reputation: 31
in javaFX i have a Pane and inside this pane another smaller pane with mouse listener. i want to detect mouse entered even if mouse is pressed. How?? ... the smaller pane does detect mouse entered but not if mouse was pressed elsewhere on the parent pane and then entered the smaller pane.
here i've written the problem in code, though i have problem in my project, the problem is the same, so i can use the solusion. but this is the problem!
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class MouseTest extends Application {
public void start(Stage primarysStage) throws Exception {
primarysStage.setTitle("Problem");
Pane wrapper = new FlowPane();
wrapper.setPrefSize(400, 400);
wrapper.setStyle("-fx-background-color: #FFFFFF");
Scene scene = new Scene(wrapper, 400, 400);
primarysStage.setScene(scene);
Pane innerPane = new Pane();
innerPane.setPrefSize(200, 200);
innerPane.setStyle("-fx-background-color: green");
EventHandler<MouseEvent> mouseEntered = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
innerPane.setStyle("-fx-background-color: black");
}
};
EventHandler<MouseEvent> mouseExited = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
innerPane.setStyle("-fx-background-color:green");
}
};
innerPane.setOnMouseEntered(mouseEntered);
innerPane.setOnMouseExited(mouseExited);
wrapper.getChildren().add(innerPane);
Label lb = new Label("\n\n\n\t\t\t\t+\n\nPress mouse here and enter the green pane:\n it does not detect!"
+ " i want it to detect mouse entered \nregardless of mouse pressed or not! HOW?");
wrapper.getChildren().add(lb);
primarysStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 0
Views: 5374
Reputation: 885
my solution is :
line.setOnMouseEntered(linehover);
line.setOnMouseExited(linehover);
add create one EventHandler for the two actions:
private EventHandler<MouseEvent> linehover = new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
// TODO Auto-generated method stub
if(event.getEventType() == MouseEvent.MOUSE_EXITED){
System.out.println("Exited");
}else if(event.getEventType() == MouseEvent.MOUSE_ENTERED){
System.out.println("Entered");
}
}
};
Upvotes: -1
Reputation: 18415
In imitation to the saying don't bring them fish, instead teach them how to fish, let me show you a trick.
Use
scene.addEventFilter(MouseEvent.ANY, e -> System.out.println( e));
to detect all mouse events and show them in the console. With this it will be obvious to you which event to catch and it will help you to solve also future problems regarding mouse events. Or any event if you use Event.ANY
.
Upvotes: 3