我正在制作一个使用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