How to listen adding a new file to a folder to show up in JTable?

257 Views Asked by At

I have a problem with my GUI. I use WatchService and it works well. How can I add this service so that JTable will show a new file?

Maybe there is another solution? Can I ask you for some examples? My table implements the AbstractTableModel.

1

There are 1 best solutions below

0
On

I resolved my problem with this, but now if i add new file my table is refreshed but they loose renderer view for jprogress bar and checkbox. I need help.

    Path path = Paths.get(filePath.getAbsolutePath());
    try {
        Boolean isFolder = (Boolean) Files.getAttribute(path, "basic:isDirectory", NOFOLLOW_LINKS);
        if (!isFolder) {
            Log.error(this, "Path: " + path + " is not a folder");
            throw new IllegalArgumentException("Path: " + path + " is not a folder");
        }
        System.out.println("Watching path: " + path);
        WatchService watchService = path.getFileSystem().newWatchService();
        path.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

        WatchKey key;
        while ((key = watchService.take()) != null) {
            for (WatchEvent<?> event : key.pollEvents()) {
                File[] files = filePath.listFiles();
                customTableDataList.clear();
                    for (File file : files) {
                        customTableData = new CustomTableData(file);
                        customTableDataList.add(customTableData);
                    }
                customTableModel = new CustomTableModel(customTableDataList);

This resolve my problem, table is reloaded but view is not rendering.

              myTable.setModel(customTableModel);

If I remove above and adding (line below), view of my table is correct but only when I click in checkbox (table is reloaded).

               myTable = new Table(customTableModel);
                myTable.repaint();

                System.out.println("#LIST: " + customTableDataList + " >>> #SIZE: " + customTableDataList.size());
            }

            //Log.info(this, "#LIST: " + customTableDataList + " >>> #SIZE: " + customTableDataList.size());
            key.reset();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.getMessage();
    }