2012-06-11 83 views
0

我遇到如下情況:帶有多個選項卡的GUI。每個選項卡都顯示我的主目錄中特定目錄中的文件內容。其中一些目錄存在於GUI創建時,其他目錄不存在。在通過FTP選項卡之一中,我得到一些文件並將其添加到主目錄。將新文件添加到目錄時更新JTabbedPane

注:我使用的是Java 5的

如何讓每個標籤知道FTP又增加了一個新的文件屬於此選項卡的目錄?

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 
    } 
} 
+0

由於您需要Java 6,因此可能是[WatchService for Java 6]的副本(http://stackoverflow.com/questions/7968488/watchservice-for-java-6)。 – trashgod

回答

4

看看在'Watching a directory'教程裏面包含了很多有用的信息,並建議使用WatchService。對於文件系統監視服務,您可以使用FileSystem#newWatchService方法

+0

我堅持使用Java 7中的java.nio.file。它不在Java 6或以上版本中,我使用java 5. – itro

+0

@itro請在您的問題中添加該信息,因爲這是對該問題的一個相當重要的限制建議的答案 – Robin

1

你可以試試這個http://jpathwatch.wordpress.com/應該使用Java 5,工作

jpatchwatch是一個Java庫,用於監視目錄更改。它使用主機平臺的本機操作系統功能來實現此目的以避免輪詢。

或者你當然可以寫你自己的投票:用了Thread.sleep等待,閱讀目錄,比較以前的列表,結果......但是,這可以讓I/O密集型的,如果你的輪詢間隔很短。另請參閱:WatchService for Java 6