Задача
Я хотел бы создать окно уведомлений, прозрачное для мыши на сцене и на рабочем столе.
Проблема
Всплывающее окно не имеет прозрачности для мыши. Когда отображается всплывающее окно, вам нужно подождать, пока оно не исчезнет, чтобы получить доступ к содержимому за ним.
Вопрос
Можно ли создавать всплывающие окна уведомлений (или другие окна) с механизмом перехода по клику? Это должно работать для всплывающих окон на сцене и всплывающих окон на рабочем столе.
Код
Вот пример кода, который создает уведомление при нажатии на кнопку. Всплывающее окно исчезает через 3 секунды. После этого вы можете нажать кнопку еще раз.
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Notification extends Application {
static double width = 800;
static double height = 600;
static int counter = 0;
Stage stage;
@Override
public void start(Stage stage) {
this.stage = stage;
// content area
StackPane root = new StackPane();
root.setStyle("-fx-background-color:white");
// button which shows a popup
Button notificationButton = new Button("Add Popup");
notificationButton.setOnAction(e -> {
addNotification("This is notification nr. " + (++counter));
});
root.getChildren().add(notificationButton);
Scene scene = new Scene(root, width, height);
stage.setScene(scene);
stage.show();
}
private void addNotification(String message) {
// create popup content
Label messageLabel = new Label(message);
final StackPane content = new StackPane();
content.setPrefSize(200, 200);
content.setStyle("-fx-background-color:lightgrey");
content.setOpacity(0.5);
content.setMouseTransparent(true);
content.getChildren().addAll(messageLabel);
// create popup and show it
final Popup popup = new Popup();
popup.setX(stage.getX() + (stage.getScene().getWidth() - 200) / 2);
popup.setY(stage.getY() + (stage.getScene().getHeight() - 200) / 2);
popup.getContent().add(content);
popup.show(stage.getScene().getWindow());
// hide popup after 3 seconds
Duration displayDuration = Duration.millis(3000);
KeyFrame displayDurationKeyFrame = new KeyFrame(displayDuration);
Timeline timeline = new Timeline(displayDurationKeyFrame);
timeline.setOnFinished(e -> {
popup.hide();
});
timeline.play();
}
public static void main(String[] args) {
launch(args);
}
}