JavaFX - 从通知弹出窗口中检索对象



我正在开发的JavaFX应用程序遇到问题,这是检索我用于创建通知弹出窗口的数据的方法。 情况是这样的:我每 x 秒对 Web 服务进行一次线程循环调用,这将返回我需要的数据(部分我用它来创建通知(。 这是代码的一部分:

if(alert.isNotificationAlertEnabled()) {
Platform.runLater(new Runnable() {
@Override
public void run() {
for(int i=0; i<result.length(); i++) {
System.out.println(".run()");
try {
Notifications notificationBuilder = Notifications.create()
   .title(((JSONObject) result.get(i)).get("number").toString())
   .hideAfter(Duration.seconds(Alert.NOTIFICATION_DURATION))
   .position(Pos.BASELINE_RIGHT)
   .text(((JSONObject) result.get(i)).get("short_description").toString())
   .darkStyle();
notificationBuilder.onAction(e -> {
// HOW TO RETRIEVE <result[i]> HERE?
});
notificationBuilder.show();
} catch(Exception e) { e.printStackTrace(); }
}
}
});
}

有没有办法将数据绑定到单个通知以便在 onAction(( 方法中使用它们? 谢谢你的时间。

也许我不明白你的问题,但在我看来你可以做到

if(alert.isNotificationAlertEnabled()) {
Platform.runLater(new Runnable() {
@Override
public void run() {
for(int i=0; i<result.length(); i++) {
System.out.println(".run()");
try {
Notifications notificationBuilder = Notifications.create()
.title(((JSONObject) result.get(i)).get("number").toString())
.hideAfter(Duration.seconds(Alert.NOTIFICATION_DURATION))
.position(Pos.BASELINE_RIGHT)
.text(((JSONObject) result.get(i)).get("short_description").toString())
.darkStyle();
notificationBuilder.onAction(e -> {
// HOW TO RETRIEVE <result[i]> HERE?
System.out.println(((JSONObject) result.get(i)).toString());
});
notificationBuilder.show();
} catch(Exception e) { e.printStackTrace(); }
}
}
});
}

if(alert.isNotificationAlertEnabled()) {
Platform.runLater(new Runnable() {
@Override
public void run() {
for(int i=0; i<result.length(); i++) {
System.out.println(".run()");
try {
JSONObject jsonObject = (JSONObject) result.get(i);
Notifications notificationBuilder = Notifications.create()
.title(jsonObject.get("number").toString())
.hideAfter(Duration.seconds(Alert.NOTIFICATION_DURATION))
.position(Pos.BASELINE_RIGHT)
.text(jsonObject.get("short_description").toString())
.darkStyle();
notificationBuilder.onAction(e -> {
// HOW TO RETRIEVE <result[i]> HERE?
System.out.println(jsonObject.toString());
});
notificationBuilder.show();
} catch(Exception e) { e.printStackTrace(); }
}
}
});
}

最新更新