如何在继续之前等待startup()中的归因完成?

发布于 2024-12-29 13:56:20 字数 1148 浏览 7 评论 0原文

我正在制作一个桌面应用程序,它使用 java.nio.file 中的 watchservice 来监视文件夹。但我需要在开始观看之前加载 gui,因为要观看的路径位于 UI 上的 JFieldText 中。

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 才能停止为空,这里我不知道是否是因为 println,但我期望它几乎是瞬时的。正常吗?

谢谢。

I'm making a desktop application which watches a folder using watchservice from java.nio.file . But I need the gui to be loaded before I start watching, because the path to be watched is in a JFieldText on the UI.

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*/ } 
}

The function getOriginPath() returns the getText() form the text field I mentioned.

In HERE0 is the attribution I mentioned. I get a nullpointer in HERE2 if gui wasn't ready.
I've tried things. If I put that thing in HERE1 it works, but of course I don't want to do that.

How could I make it?

And its taking to long(like two seconds) or the gui to stop being null with this HERE1 I don't know if it is because of the println, but I was expecting it to be almost instantaneous. Is it normal?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风追烟花雨 2025-01-05 13:56:20

鉴于发布的信息有限,我必须做出一些假设。假设 1 是您为 JTextField 指定一个默认值,并将其用作您要观看的文件的路径。假设 2 是您在编码时没有着眼于类似 MVC 的设计。

如果两者都正确,那么听起来就像是尾巴摇着狗一样——保存关键数据的视图,而不是模型。为什么不通过转向 MVC 来解决您的问题,而不是从视图中获取关键数据,而是从模型中获取关键数据。首先启动模型,包括从程序属性中获取默认路径,启动监听器,启动视图,然后如果视图要求控制器更改监视的文件,则让控制器更改模型。然后模型中的侦听器将通知您的任何观察者更改。

Given the limited information posted, I have to make some assumptions. Assumption 1 is that you give the JTextField a default value and use that as the path to the file you wish to watch. Assumption 2 is that you have not coded with an eye towards MVC-like design.

If both are correct, then it sounds like you have the tail wagging the dog -- the view holding the critical data, not the model. Why not fix your problem by going towards MVC and not getting the critical data from the view but rather from the model. Start the model up first thing, including getting the default path from your program Properties, get your listener going, start your view, and then if the view asks the controller to change the watched file, have the controller change the model. And then listeners in the model will notify your any observers of change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文