JavaFX - получение объектов из всплывающих уведомлений - PullRequest
0 голосов
/ 15 октября 2018

У меня проблема с приложением JavaFX, которое я разрабатываю. Это способ извлечения данных, которые я использую для создания всплывающего уведомления.Дело в следующем: у меня есть вызов циклического потока для веб-службы каждые x секунд, это возвращает мне данные, которые мне нужны (которые, в частности, я использую для создания уведомления).Это часть кода:

        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 ()?Спасибо за ваше время.

1 Ответ

0 голосов
/ 15 октября 2018

Может быть, я не понимаю вашего вопроса, но мне кажется, вы можете сделать

 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(); }

            }
        }
    });
}
...