JavaFX ScheduledService的updateMessage未按预期更新问题求助
问题描述
开发的JavaFX应用整体运行正常,但ScheduledService的updateMessage方法出现异常行为。原本预期每次调用updateMessage都会更新messageProperty(每5秒执行一次),但实际并未生效,而updateValue和System.out.println均能正常工作。需要确认这是组件故障还是对updateMessage的理解存在偏差。
代码实现
Main类
import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.concurrent.ScheduledService; import javafx.concurrent.Task; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.util.Duration; import org.controlsfx.control.Notifications; public class App extends Application { static List<MyNotification> myNotifications = new ArrayList(); static FakeDatabaseHandler fakeDatabaseHandler = new FakeDatabaseHandler(); @Override public void start(Stage stage) { Button btnGenerateNotification = new Button("Generate!"); btnGenerateNotification.setOnAction(actionEvent ->{ fakeDatabaseHandler.addFakeNotificationsToDb(); }); Label lblUpdateMessage = new Label("Update Message: "); ScheduledService<List<MyNotification>> checkForScheduledService = new CheckForNotificationsScheduledService(); checkForScheduledService.setPeriod(Duration.seconds(5)); checkForScheduledService.messageProperty().addListener((ov, oldValue, newValue) -> { if(newValue != null) { lblUpdateMessage.setText("Update Message: " + newValue); } }); checkForScheduledService.valueProperty().addListener((ov, oldValue, newValue) -> { if(newValue != null && !newValue.isEmpty()) { myNotifications.clear(); myNotifications.addAll(newValue); checkForScheduledService.cancel(); AtomicInteger index = new AtomicInteger(0); checkForScheduledService.cancel(); Timeline threeSecondsWonder = new Timeline( new KeyFrame(Duration.seconds(3), event -> { MyNotification myNotification = myNotifications.get(index.getAndIncrement()); Notifications.create().title(myNotification.department()+ " - " + myNotification.title()).text(myNotification.message()).hideAfter(Duration.seconds(3)).showWarning(); }) ); threeSecondsWonder.setCycleCount(myNotifications.size()); threeSecondsWonder.play(); threeSecondsWonder.setOnFinished((t) -> { checkForScheduledService.restart(); }); } }); checkForScheduledService.start(); StackPane root = new StackPane(new VBox(btnGenerateNotification, lblUpdateMessage)); stage.setScene(new Scene(root, 500, 500)); stage.setTitle("Facilities"); stage.show(); } public static void main(String[] args) { launch(args); } private static class CheckForNotificationsScheduledService extends ScheduledService<List<MyNotification>> { @Override protected Task<List<MyNotification>> createTask() { return new Task<List<MyNotification>>() { @Override protected List<MyNotification> call() throws Exception { List<MyNotification> fakeNotificationList = fakeDatabaseHandler.getFakeNotificationsFromDB(); System.out.println("Checking for notifications. Notifications Found: " + fakeNotificationList.size()); updateMessage("Checking for notifications. Notifications Found: " + fakeNotificationList.size()); updateValue(fakeNotificationList); return fakeNotificationList; } }; } } }
FakeDatabaseHandler类
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.ThreadLocalRandom; /** * * @author Sed */ public class FakeDatabaseHandler { private final List<MyNotification> fakeDBData = new ArrayList(); public FakeDatabaseHandler() {} public void addFakeNotificationsToDb(){ List<String> departmentList = Arrays.asList("A", "B", "C", "D"); for(int i = 0; i < ThreadLocalRandom.current().nextInt(1, 6); i++) { int notificationID = ThreadLocalRandom.current().nextInt( 1000000); fakeDBData.add(new MyNotification("Department: " + departmentList.get(ThreadLocalRandom.current().nextInt(4)), "Title " + notificationID, "Message: " + notificationID)); } } public List<MyNotification> getFakeNotificationsFromDB(){ List<MyNotification> returnList = new ArrayList(fakeDBData); fakeDBData.clear(); return returnList; } }
MyNotification记录类
public record MyNotification(String department, String title, String message) {}
Module-Info
module com.mycompany.javafxsimpletest { requires org.controlsfx.controls; exports com.mycompany.javafxsimpletest; }
输出效果

技术信息
Product Version: Apache NetBeans IDE 18 Java: 19.0.2; OpenJDK 64-Bit Server VM 19.0.2+7-jvmci-22.3-b12 Runtime: OpenJDK Runtime Environment 19.0.2+7-jvmci-22.3-b12 System: Windows 11 version 10.0 running on amd64; UTF-8; en_US (nb)
内容的提问来源于stack exchange,提问作者SedJ601
相关产品推荐
相关产品推荐

