gpt4 book ai didi

java - 监听来自 JavaFX 服务的 SimpleListProperty 更新

转载 作者:行者123 更新时间:2023-12-02 03:39:52 27 4
gpt4 key购买 nike

我有一个 JavaFX 服务,并且希望在该服务更新自定义属性时通过回调收到通知。通常,完成此操作的方式是服务具有一些内置属性,例如“state”、“workDone”、“value”等。与我要模拟的属性最相似的是“value”命名属性。 “value”属性在服务类中定义如下。

    private final ObjectProperty<V> value = new SimpleObjectProperty<>(this, "value");
@Override public final V getValue() { checkThread(); return value.get(); }
@Override public final ReadOnlyObjectProperty<V> valueProperty() { checkThread(); return value; }


In my case I needed to listen to updates to a pair of message objects being updated by my JavaFX service thread, so I added a custom named property for each one as shown below.

// add 'heartbeat' bound property
private final ObjectProperty<CAServiceHeartbeatMessage> heartbeat =
new SimpleObjectProperty<>(this, "heartbeat", null);
public CAServiceHeartbeatMessage getHeartbeat() {
return heartbeat.get();
}
public void setHeartbeatMessage(CAServiceHeartbeatMessage aHeartbeat) {
heartbeat.set(aHeartbeat);
}
public ObjectProperty<CAServiceHeartbeatMessage> heartbeatProperty() {
return heartbeat;
}

// add 'memberSystemStatus' bound property
private final ObjectProperty<MemberSystemStatusMessage> memberSystemStatus =
new SimpleObjectProperty<>(this, "memberStatus", null);
public MemberSystemStatusMessage getMemberStatus() {
return memberSystemStatus.get();
}
public void setMemberSystemMessage(MemberSystemStatusMessage aMemberSystemStatus) {
memberSystemStatus.set(aMemberSystemStatus);
}
public ObjectProperty<MemberSystemStatusMessage> memberSystemStatusProperty() {
return memberSystemStatus;
}

然后在 Controller 类中,我添加了以下代码,以根据对每个属性的更改来更新 GUI:

// heartbeatMessageProperty listener
// Update the GUI widgets when a new CAServiceHeartbeatMessage (heartbeatMessage) is updated
mListenerService.heartbeatProperty().addListener(
(ObservableValue<? extends CAServiceHeartbeatMessage> observer,
CAServiceHeartbeatMessage oldValue, CAServiceHeartbeatMessage newValue) -> {
// this is where we need to intelligently update the appropriate
// heartbeat service entries
System.out.println("updated CAServiceHeartbeatMessage");
});

// 'memberSystemMessage' bound property listener
mListenerService.memberSystemStatusProperty().addListener(
(ObservableValue<? extends MemberSystemStatusMessage> observer,
MemberSystemStatusMessage oldValue, MemberSystemStatusMessage newValue) -> {
if (newValue == null) {

。 。 .

我的问题是,如何在 JavaFX Service 类中替换多个 SimpleObjectProperties 以使用单个 SimpleListPropertySimpleMapProperty 。我不确定如何或是否需要遵循某些属性命名约定来在我的线程中公开一些简单的列表属性,就像我对 2 个特定且单独命名的消息属性所做的那样。值得注意的是,这些消息共享一个公共(public)的基本消息类 - 因此它们可以存储在简单的列表类型中。我还想看看如何处理更新的示例,以便在发生更改时更新 GUI,特别是我不知道如何确定列表或 map 中的哪些元素发生了更改。

最佳答案

要监听列表中元素的变化,您可以使用 FXCollections 的工厂方法初始化 ObservableList:

 ObservableList<Message> messages = FXCollections.observableArrayList(message -> new Observable[] { message.messageProperty() });

Creates a new empty observable list backed by an arraylist. This list reports element updates.

或者:

FXCollections.observableList(backingList, message -> new Observable[] { message.messageProperty() });

Constructs an ObservableList that is backed by the specified list. Mutation operations on the ObservableList instance will be reported to observers that have registered on that instance. Note that mutation operations made directly to the underlying list are not reported to observers of any ObservableList that wraps it. This list also reports mutations of the elements in it by using extractor. Observable objects returned by extractor (applied to each list element) are listened for changes and transformed into "update" change of ListChangeListener.

javadoc

 public void changeListener() {

ObservableList<Message> messages = FXCollections.observableArrayList(message -> new Observable[] { message.messageProperty() });

messages.addListener((ListChangeListener<Message>) c -> {

while (c.next()) {
LOGGER.debug("change: {}", c);

if (c.wasAdded()) {
List<? extends Message> addedSubList = c.getAddedSubList();
}
if (c.wasUpdated()) {
int from = c.getFrom();
int to = c.getTo();
List<? extends Message> subList = c.getList().subList(from, to);
}
}
});

HearBeat beat = new HearBeat("beat");

messages.add(beat);
messages.add(new Status("status"));

beat.setMesssage("beat changed");
beat.setMesssage("beat changed again");

messages.addAll(new Status("1"), new Status("2"));
}

public abstract class Message {

private StringProperty message;

Message(String message) {
this.message = new SimpleStringProperty(message);
}

public StringProperty messageProperty() {
return message;
}

public String getMesssage() {
return message.get();
}

public void setMesssage(String val) {
message.set(val);
}
}

public class HearBeat extends Message {

public HearBeat(String message) {
super(message);
}
}

public class Status extends Message {

Status(String message) {
super(message);
}
}

关于java - 监听来自 JavaFX 服务的 SimpleListProperty<T> 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36959364/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com