Search code examples
javaswingjtabbedpanejava-5changelistener

Update JTabbedPane when new file added to directory


I have situation as follows: GUI with several tabs. Every tab shows content of files in the specific directory in my home directory. Some of these directories exist at the point of GUI creation and others do not. In one of tab via FTP I get some file and add it to the home directory.

Note: I'm using java 5.

How to let every tab know that FTP has added a new file to the directory which belongs to this tab?

public class ScannerPanel extends JPanel implements ChangeListener {

    private void initGUI()      {
        setPreferredSize(new Dimension(700, 400));
        JTabbedPane tabbedPane  = new JTabbedPane();

        tabbedPane.addTab("FTP", IconLib.ICON_16X16_DATA,new FtpTabPanel (this),"FTP");
        tabbedPane.addTab("PCS", IconLib.ICON_16X16_THUM,new PcsTabPanel (this),"PCS");
        tabbedPane.addTab("Car", IconLib.ICON_16X16_PAPE,new CarTabPanel (this),"Car");
        tabbedPane.addTab("Pic", IconLib.ICON_16X16_RETI,new PicTabPanel (this),"Pic");

        tabbedPane.addChangeListener(this);
        add(tabbedPane);
    }

    public void stateChanged(ChangeEvent e) {
           //detect new file has been added to directory
          // update the content of tab
    }
}

Solution

  • Take a look at the 'Watching a directory' tutorial which contains a lot of useful information and recommends to use a WatchService. For a file system watch service, you can use the FileSystem#newWatchService method