JavaFX中的自定义ListView不起作用

时间:2016-08-09 07:17:35

标签: java listview javafx

我正在使用java开发聊天应用程序并使用JavaFX开发GUI。我在自定义ListView中显示消息时遇到问题。我使用setcellfactory在自定义列表视图中设置值。

当我将值静态设置为ListView时,它可以正常工作。但是当我通过ObservableList动态地将值设置到列表视图时,它会在列表中重复相同值的单元格

这是我的代码:

package sample;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.util.Callback;

import javax.swing.*;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    public Button send;
    @FXML
    public Label botName;
    @FXML
    public TextField message;
    @FXML
    public ListView<String> oldMessage, botList;
    @FXML
    public ListView<String> chatWindow;
    ObservableList<String> items, chatcontent;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        File file = new File("C:\\ab\\bots");
        String[] names = file.list();
        items = FXCollections.observableArrayList(names);
        oldMessage.setItems(items);
        botList.setItems(items);
        //default selection
        botList.getSelectionModel().select(0);
        botName.setText(botList.getSelectionModel().selectedItemProperty().getValue());
        botList.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                botName.setText(newValue);
            }
        });
        botList.setCellFactory(new Callback<ListView<String>, javafx.scene.control.ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> listView) {
                return new ListViewCell();
            }
        });

        //send message
        message.setOnAction(e -> {
            String c = message.getText().toString();
            chatcontent.add(c);
            chatWindow.scrollTo(c);
            chatWindow.refresh();
            message.clear();
        });
        send.setOnAction(e -> {
            String c = message.getText().toString();
            chatcontent.add(c);
            chatWindow.scrollTo(c);
            chatWindow.refresh();
            message.clear();
        });
        setListView();
    }

    public void setListView() {
        chatcontent = FXCollections.observableArrayList(items);
        chatWindow.setItems(chatcontent);
        chatWindow.setCellFactory(new Callback<ListView<String>, javafx.scene.control.ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> listView) {
                return new ListViewCellChat();
            }
        });
    }

    static class ListViewCellChat extends ListCell<String> {

        @Override
        public void updateItem(String string, boolean empty) {
            super.updateItem(string, empty);
            if (string != null) {
                Data1 data = new Data1();
                data.setInfo(string);
                setGraphic(data.getBox());
            }
        }
    }

}

0 个答案:

没有答案