-1

Global task: to make a cloud manager. I need help. javaFX + Netty, How to make sure that the table is updated ONLY AFTER information arrives from the server? I understand that I need to play with thread, but I can't get the result.

public void updateList(Path path) {
    try {
        String correctPath = getCorrectPath(path);
        pathField.setText(correctPath);
        filesTable.getItems().clear();
        if (!correctPath.startsWith(CLOUD)) {
            filesTable.getItems().addAll(Files.list(path).map(FileInfo::new).collect(Collectors.toList()));
        } else {
            List<FileInfo> fileInfoList = null;
                fileInfoList = files.get(userName);
            filesTable.getItems().addAll(files.get(userName));
            files.remove(userName);
        }
        filesTable.sort();
    } catch (Exception e) {
        Alert alert = new Alert(Alert.AlertType.WARNING, "По какой-то причине не удалось обновить список файлов", ButtonType.OK);
        alert.showAndWait();
    }
}

....

 @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(msg);
        if (msg.startsWith(ROOT)) {
            String[] commands = msg.split(" ");
            String jsonString = msg.substring(ROOT.length()).substring(commands[1].length()).substring(FILE_INFO.length()).trim();
            files.put(userName, getFileInfos(jsonString));
        }
    }

.....

private List<FileInfo> getFileInfos(String jsonString) {
    Gson g = new Gson();
    List<FileInfo> fileInfoList = new CopyOnWriteArrayList<>();
    String[] fileInfos = jsonString.split("/");
    FileInfo fileInfo;
    for (int i = 0; i < fileInfos.length - 1; i++) {
        fileInfo = g.fromJson(fileInfos[i].substring(1).trim(), FileInfo.class);
        fileInfoList.add(fileInfo);
    }
    return fileInfoList;
}
framzik
  • 13
  • 3
  • 1
    The question is not very clear, but see if https://stackoverflow.com/questions/30249493/using-threads-to-make-database-requests/30250308#30250308 helps. That post is about database requests, but essentially it's the same thing: retrieve data from over a network in a background thread and update the UI when it's complete. – James_D May 27 '21 at 19:33

0 Answers0