Java聊天-更新Java FX中不同listViews的消息

时间:2018-11-25 15:39:21

标签: java sockets listview javafx service

我正在尝试使用不同的observableLists(一个observableList =一个聊天)更新listView,消息应该动态显示。它适用于第一次单击的聊天-当我更改聊天时-一些消息会出现在BAD聊天中-在不同的observableList中。我以为-当我调用messageRenderingService.restart();时-它将再次使用新参数启动该服务,但是它没有按预期工作。

public class Controller extends AbstractController implements Initializable {

private Client client;
private Client selectedClient;
private ObservableList<String> observableList = FXCollections.emptyObservableList();

@FXML
private TreeView<Client> treeView;
private TreeItem<Client> root  = new TreeItem<Client>();

@FXML
private ListView<String> mainChat;

List<ListView<String>> listViews = new ArrayList<>();

@FXML
private TextField sendField;

@FXML
void sendMessage(ActionEvent event) {
    String msg = sendField.getText();
    if(msg != null){
        client.sendMessage(new Message(ServerProtocols.PRIVATE, selectedClient.getId(), selectedClient.getName(), msg));
    }
}

public Controller(AccessData accessData) {
    super(accessData);
}

@Override
public void initialize(URL location, ResourceBundle resources) {
    client = getAccessData().getActiveClient();

    treeView.setRoot(root);

    root.setExpanded(true);

    OnlineUsersService onlineUsersService = new OnlineUsersService(client, root, getAccessData(), selectedClient);
    onlineUsersService.start(); 

    treeView.setOnMouseClicked(e -> {
        selectedClient = treeView.getSelectionModel().getSelectedItem().getValue();
        if(selectedClient != null){
            getAccessData().addChat(client.getId(), selectedClient.getId());
            ObservableList<String> chat = getAccessData().getActiveChat();
            System.out.println("Active chat is..." +  chat);
            MessageCheckingService messageCheckingService = new MessageCheckingService(client, getAccessData().getActiveChat(), mainChat);
            messageCheckingService.restart();
        }
    });


}

}

public class AccessData {

private Client activeClient;
private ObservableList<String> activeChat;

private Map<String, ObservableList<String>> chats = new HashMap<>();


public Client getActiveClient() {
    return activeClient;
}

public void setActiveClient(Client activeClient) {
    this.activeClient = activeClient;
}

public ObservableList<String> getChat(int id1, int id2) {
    String value = convertChatID(id1, id2);
    System.out.println("CHCI CHAT S ID " + value);
    if (chats.containsKey(value)) {
        return chats.get(value);
    }
    return null;
}

public void addChat(int id1, int id2) {
    String value = convertChatID(id1, id2);
    System.out.println("Nastavuji view---" + value);
    if (!chats.containsKey(value)) {
        ObservableList<String> chat = FXCollections.observableArrayList();
        System.out.println("VKLÁDÁM " + value + " - " + chat);
        chats.put(value, chat);
        setActiveChat(chat);
    } else {
        System.out.println("Chat alredy IN");
        setActiveChat(chats.get(value));
    }
}

public ObservableList<String> getActiveChat(){
    return activeChat;
}

public void setActiveChat(ObservableList<String> chat) {
    this.activeChat = chat;
}

private String convertChatID(int id1, int id2) {
    String value = "";
    // 1; 2
    // 2; 1
    if (id1 < id2) {
        value = id1 + "" + id2;
    } else if (id1 > id2) {
        value = id2 + "" + id1;
    }
    return value;
}

}

public class MessageCheckingService extends Service<Void> {

private ListView<String> listView;
private Client client;
private ObservableList<String> messages;

public MessageCheckingService(Client client,ObservableList<String> observableList, ListView<String> listView){
    this.client = client;
    this.messages = observableList;
    this.listView = listView;

    listView.setItems(messages);
}

@Override
protected Task<Void> createTask() {
    System.out.println("STARTING NEW MESSAGECHECKING SERVICE!");
    return new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            while(true){
                Message m = client.getLastMessage();
                System.out.println("MESSAGE " + m.getMessage());
                Platform.runLater(() -> {
                    messages.add(m.getMessage());
                });
                System.out.println("OBSERVABLE LIST IN MESSAGESERVICE" + messages);
            }
        }
    };
}

}

0 个答案:

没有答案