套接字聊天-如何为Java fx中的每个私人对话创建listView?

时间:2018-11-23 09:13:52

标签: java listview javafx socket.io chat

如何为每个对话更改listView? 当我点击TreeItem-并在其旁边时,我有listView-我想更改listView,以进行具有正确数据的正确对话(因此具有正确客户端的消息)-此时,我只有一个listView,并且所有消息仅显示在一个listView中...我该怎么做? 谢谢

控制器

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;

    @FXML
    private TextField sendField;

    @FXML
    void sendMessage(ActionEvent event) {
        String msg = sendField.getText();
        System.out.println("MESSAGE " + msg);
        if (msg != null) {
            System.out.println("POSÍLÁM ZPRÁVU!!!");
            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);
        onlineUsersService.start(); // PŘIDÁVÁ ONLINE PŘÁTELÉ

        treeView.setOnMouseClicked(e -> {
            selectedClient = treeView.getSelectionModel().getSelectedItem().getValue();
            if(selectedClient != null){
                System.out.println(observableList.size() + "OBS LIST SIZE");
            }
        });

        MessageCheckingService messageCheckingService = new MessageCheckingService(client, observableList, mainChat);
        messageCheckingService.start();
    }
}

MessageCheckingService

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() {
        return new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                while (true) {
                    Message m = client.getLastMessage();
                    System.out.println("MESSAGE " + m.getMessage());
                    messages.add(m.getMessage());
                }
            }
        };
    }
}

0 个答案:

没有答案