Reputation: 66
I have a problem. If I set an effect to a pop up rectangle, the effect will be applied again and again. So it will be very thick... What should I do? Thanks for help!
@Override
public void start(final Stage stage) {
stage.setTitle("PopupXmpl");
BorderPane root = new BorderPane();
final Popup pop = new Popup();
Circle circle = new Circle(400, 300, 200, Color.WHITESMOKE);
circle.setStroke(Color.BLACK);
circle.setOnMouseEntered(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent t) {
Rectangle rectangle = new Rectangle(40, 15, Color.WHITE);
rectangle.setStroke(Color.DARKGREY);
rectangle.setArcHeight(4);
rectangle.setArcWidth(6);
pop.setHeight(100);
pop.setWidth(100);
pop.setX(t.getScreenX());
pop.setY(t.getScreenY()-50);
rectangle.setEffect(new DropShadow());
pop.getContent().add(rectangle);
pop.show(stage);
}
});
circle.setOnMouseExited(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent t) {
pop.hide();
}
});
root.getChildren().add(circle);
stage.setScene(new Scene(root, 800, 600));
stage.show();
}
Upvotes: 0
Views: 491
Reputation: 49205
You are adding the rectangle to the popup's content over and over again on every MouseEntered
event. Add it only once and change only event related properties of popup on this MouseEntered
event:
@Override
public void start(final Stage stage) {
stage.setTitle("PopupXmpl");
BorderPane root = new BorderPane();
Rectangle rectangle = new Rectangle(40, 15, Color.WHITE);
rectangle.setStroke(Color.DARKGREY);
rectangle.setArcHeight(4);
rectangle.setArcWidth(6);
rectangle.setEffect(new DropShadow());
final Popup pop = new Popup();
pop.getContent().add(rectangle);
pop.setHeight(100);
pop.setWidth(100);
Circle circle = new Circle(400, 300, 200, Color.WHITESMOKE);
circle.setStroke(Color.BLACK);
circle.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
pop.setX(t.getScreenX());
pop.setY(t.getScreenY() - 50);
pop.show(stage);
}
});
circle.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
pop.hide();
}
});
root.getChildren().add(circle);
stage.setScene(new Scene(root, 800, 600));
stage.show();
}
Upvotes: 1