gpt4 book ai didi

Javafx ListView动态更新

转载 作者:行者123 更新时间:2023-12-02 10:55:48 26 4
gpt4 key购买 nike

我正在使用 JavaFx 创建一个聊天应用程序。我的好友列表已加载到 ListView 中。我的要求是当用户收到 friend 的消息时显示通知图标。为此,我需要知道 friend 所在的单元格,并且还需要显示通知图标。我无法找到一种方法来做到这一点,因为我对 FX 很陌生。任何帮助将不胜感激。

enter image description here谢谢

最佳答案

将您收到通知的好友信息存储在某个可观察对象中。这可能是项目类本身

public class Friend {

private final IntegerProperty messageCount = new SimpleIntegerProperty();

public int getMessageCount() {
return messageCount.get();
}

public void setMessageCount(int value) {
messageCount.set(value);
}

public IntegerProperty messageCountProperty() {
return messageCount;
}

...

}

或外部数据结构,如以下示例中使用的 ObservableMap:

public class Friend {

private final String name;

public String getName() {
return name;
}

public Friend(String name) {
this.name = name;
}

}
@Override
public void start(Stage primaryStage) {
// map storing the message counts by friend
final ObservableMap<Friend, Integer> messageCount = FXCollections.observableHashMap();

ListView<Friend> friendsListView = new ListView<>();
friendsListView.setCellFactory(lv -> new ListCell<Friend>() {
final StackPane messageNotification;
final Text numberText;
final InvalidationListener listener;

{
// notification item (white number on red circle)
Circle background = new Circle(10, Color.RED);

numberText = new Text();
numberText.setFill(Color.WHITE);

messageNotification = new StackPane(background, numberText);
messageNotification.setVisible(false);

listener = o -> updateMessageCount();
setGraphic(messageNotification);
}

void updateMessageCount() {
updateMessageCount(messageCount.getOrDefault(getItem(), 0));
}

void updateMessageCount(int count) {
boolean messagesPresent = count > 0;
if (messagesPresent) {
numberText.setText(Integer.toString(count));
}
messageNotification.setVisible(messagesPresent);

}

@Override
protected void updateItem(Friend item, boolean empty) {
boolean wasEmpty = isEmpty();
super.updateItem(item, empty);
if (wasEmpty != empty) {
if (empty) {
messageCount.removeListener(listener);
} else {
messageCount.addListener(listener);
}
}

if (empty || item == null) {
setText("");
updateMessageCount(0);
} else {
setText(item.getName());
updateMessageCount();
}

}

});

Random random = new Random();
List<Friend> friends = Stream
.of(
"Sheldon",
"Amy",
"Howard",
"Bernadette",
"Lennard",
"Penny")
.map(Friend::new)
.collect(Collectors.toCollection(ArrayList::new));

friendsListView.getItems().addAll(friends);

List<Friend> messages = new ArrayList(friends.size() * 2);

// 2 messages for each friend in random order
Collections.shuffle(friends, random);
messages.addAll(friends);
Collections.shuffle(friends, random);
messages.addAll(friends);

// demonstrate adding/removing messages via timelines
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {

Iterator<Friend> iterator = messages.iterator();

@Override
public void handle(ActionEvent event) {
messageCount.merge(iterator.next(), 1, Integer::sum);
}

}));
timeline.setCycleCount(messages.size());

Timeline removeTimeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {

Iterator<Friend> iterator = messages.iterator();

@Override
public void handle(ActionEvent event) {
messageCount.merge(iterator.next(), 1, (a, b) -> a - b);
}

}));
removeTimeline.setCycleCount(messages.size());

new SequentialTransition(timeline, removeTimeline).play();

Scene scene = new Scene(friendsListView);

primaryStage.setScene(scene);
primaryStage.show();
}

对于存储在 Friend 类中的消息计数,您需要修改注册监听器并稍微更新单元格。

listener = o -> updateMessageCount(getItem().getMessageCount());
@Override
protected void updateItem(Friend item, boolean empty) {
Friend oldItem = getItem();
if (oldItem != null) {
oldItem.messageCountProperty().removeListener(listener);
}

super.updateItem(item, empty);

if (empty || item == null) {
setText("");
updateMessageCount(0);
} else {
setText(item.getName());
item.messageCountProperty().addListener(listener);
updateMessageCount(item.getMessageCount());
}

}

关于Javafx ListView动态更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51765043/

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