문제

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
    }
}
도움이 되었습니까?

해결책

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

다른 팁

You can try http://jpathwatch.wordpress.com/ this one should work with Java 5.

jpatchwatch is a Java library for monitoring directories for changes. It uses the host platform’s native OS functions to achieve this to avoid polling.

Or of course you could write your own polling: wait with Thread.sleep, read the directory, compare the results with the previous list... But this can get I/O intensive, if your polling interval is very short. Also see this: WatchService for Java 6

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top