JavaFXListView动态更新

JavaFXListView动态更新,java,javafx,fxml,Java,Javafx,Fxml,我正在用JavaFx创建一个聊天应用程序。我的朋友列表已加载到列表视图中。我的要求是在用户收到朋友的消息时显示通知图标。为此,我需要知道朋友所在的手机,还需要显示通知图标。我想不出一个办法来做这件事;我对外汇很陌生。 任何帮助都将不胜感激 谢谢将通知您的朋友的信息存储在一些可观察的数据库中。这可能是item类本身 public class Friend { private final IntegerProperty messageCount = new SimpleIntegerPr

我正在用JavaFx创建一个聊天应用程序。我的朋友列表已加载到列表视图中。我的要求是在用户收到朋友的消息时显示通知图标。为此,我需要知道朋友所在的手机,还需要显示通知图标。我想不出一个办法来做这件事;我对外汇很陌生。 任何帮助都将不胜感激


谢谢

将通知您的朋友的信息存储在一些可观察的数据库中。这可能是item类本身

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

}

使用单元渲染。您可以将场景(FXML)显示为列表单元格值。您好,谢谢。你能给我举个好例子吗?我有点迷路了当然…………嗨,谢谢你。我试着这样做,但因为我已经为朋友的显示图片设置了图形,我对此感到困惑。如何处理这两种图形?我用图片再次编辑了我的问题。。你能解释一下布尔wasEmpty=isEmpty()吗;super.updateItem(项,空);if(wasEmpty!=empty){if(empty){messageCount.RemovelListener(listener);}else{messageCount.addListener(listener);}}如果单元格从空更改为非空,或者反过来,根据更新后空属性的状态添加或删除侦听器。(空单元格不需要更新,因为无论贴图的状态如何,它们始终保持为空)
@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();
}
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());
    }

}