我正在製作一個使用java.nio.file中的watchservice觀察文件夾的桌面應用程序。但是我需要在開始觀看之前加載gui,因爲要觀看的路徑位於UI上的JFieldText中。如何在繼續之前等待startup()中的屬性完成?
public class FileArchiverApp extends SingleFrameApplication {
static FileArchiverView gui;
@Override protected void startup() {
gui = new FileArchiverView(this); //HERE0 I have to wait for this.
show(gui);
...
public static void main(String[] args) throws IOException {
launch(FileArchiverApp.class, args);
....
WatchService watcher = FileSystems.getDefault().newWatchService();
// HERE1 while(gui==null) System.out.println("hi") ;
try {
Path dir = Paths.get(gui.getOriginPath()); // HERE2 I get nullpointer if gui was not ready
WatchKey key = dir.register(watcher, ENTRY_CREATE);
} catch (Exception x) {
System.err.println(x);
}
while(true){ /*wait for new file event loop*/ }
}
函數getOriginPath()從我提到的文本字段返回getText()。
在HERE0中是我提到的屬性。如果gui沒有準備好,我會在HERE2中獲得一個空指針。 我試過了。如果我把它放在HERE1中,它可以工作,但我當然不想那麼做。
我該怎麼做?
而且它需要很長的時間(比如兩秒鐘)或者gui才能停止與這個HERE1無效,我不知道是不是因爲println,但我期待它幾乎是瞬間的。這是正常的嗎?
謝謝。
爲什麼使用基於JSR 296的'SingleFrameApplication'?它沒有被遺棄嗎?爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-01-29 05:01:46