Swing 更新谜题

发布于 2024-10-16 16:39:09 字数 671 浏览 3 评论 0原文

我有一个 Jtree,其中的节点代表用户可以调用和查看的图像。有时加载图像需要四到五秒的时间。理想情况下,我希望在用户等待时显示等待光标,并选择树中选择的节点。然而,我发现用户单击该节点,似乎没有发生任何事情,然后出现图像,然后选择该节点(等待光标永远不会出现或更可能出现非常短暂,然后立即消失。我'我尝试重新绘制树和小程序,以尝试强制行为按我想要的顺序发生,到目前为止,我没有任何建议? 以下是一些给我带来问题的 Swing 代码的一部分:

 thisApplet.setCursor(new Cursor(Cursor.WAIT_CURSOR));
 selectdocumentTreeLeaf(); // a JTree with nodes representing images                  
 tree.repaint();
 thisApplet.repaint();
 tree.setEnabled(false); //disabled so users don't keep clicking on it.   
 result = createImage(queue.q[pointer].currentPage); //where the image is fetched              
 thisApplet.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

I have a Jtree with nodes representing images that the user can call up and view. Sometimes it takes the images up to four or five seconds to load. Ideally, I would like wait cursor to show while the user is waiting and the node selected in the tree to be selected. However, what I'm find is that the user clicks on the node, nothing appears to happen and then the image appears and then the node is selected (the wait cursor never appears or more likely appears very briefly and then disappears immediately. I've tried repainting both the tree and the applet to try to force the behavior to happen in the sequence I want. So far, I've not had any luck. Any suggestions?
Here is a section of some Swing code that is giving me problems:

 thisApplet.setCursor(new Cursor(Cursor.WAIT_CURSOR));
 selectdocumentTreeLeaf(); // a JTree with nodes representing images                  
 tree.repaint();
 thisApplet.repaint();
 tree.setEnabled(false); //disabled so users don't keep clicking on it.   
 result = createImage(queue.q[pointer].currentPage); //where the image is fetched              
 thisApplet.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

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

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

发布评论

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

评论(1

独自唱情﹋歌 2024-10-23 16:39:09

我认为佩斯是有钱的。使用后台线程(例如 SwingWorker),您的问题将得到解决。请查看Swing 中的并发,了解有关 EDT 和线程问题。例如,

  thisApplet.setCursor(new Cursor(Cursor.WAIT_CURSOR));
  selectdocumentTreeLeaf(); 
  tree.repaint();
  thisApplet.repaint();
  tree.setEnabled(false); 

  new SwingWorker<Image, Void>(){
     @Override
     protected Image doInBackground() throws Exception {
        return createImage(queue.q[pointer].currentPage);;
     }

     @Override
     protected void done() {
        try {
           result = get();
           thisApplet.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
           tree.setEnabled(true);
        } catch (InterruptedException e) {
           e.printStackTrace();
        } catch (ExecutionException e) {
           e.printStackTrace();
        }
     }
  }.execute();

I think Pace is on the money. Use a background thread such as a SwingWorker and your problems will be solved. Please check out Concurrency in Swing for more details on the EDT and thread problems. e.g.,

  thisApplet.setCursor(new Cursor(Cursor.WAIT_CURSOR));
  selectdocumentTreeLeaf(); 
  tree.repaint();
  thisApplet.repaint();
  tree.setEnabled(false); 

  new SwingWorker<Image, Void>(){
     @Override
     protected Image doInBackground() throws Exception {
        return createImage(queue.q[pointer].currentPage);;
     }

     @Override
     protected void done() {
        try {
           result = get();
           thisApplet.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
           tree.setEnabled(true);
        } catch (InterruptedException e) {
           e.printStackTrace();
        } catch (ExecutionException e) {
           e.printStackTrace();
        }
     }
  }.execute();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文