2011-02-23 69 views
2
.addActionListener(new ActionListener(){ 
     public void actionPerformed (ActionEvent e){ 
      try{ 
       ta.append("Searching Initiated at: "+datetime()+"\n"); 
       gui.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 
       task.execute(); 
       javax.swing.SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
         gui.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 
        } 
       }); 
       //Enable the next stage in the YD process and disable the previously executed functions 
       clusAn.setEnabled(true); 
       open.setEnabled(false); 
       statCl.setEnabled(false); 
      }catch (Exception IOE){ 
       } 
     } 
    }); 

嗨,在这个应用程序的最后一个阶段,我设计了一点痛苦。Java SwingUtilities.invokeLater

基本上,当用户点击按钮时,我希望它使光标变成'等待'版本,然后一旦后台进程(task.execute)完成,光标将恢复正常。

task.execute不在同一个类中,所以我不能直接调用“gui.setCursor”,因为它不会将GUI识别为变量。

不知道该怎么办,所以任何建议将是巨大的

感谢:d

+2

您不需要'actionPerformed()'中的'invokeLater()',因为它总是从GUI事件派发线程中调用。 –

回答

5

修改任务的类,以便将GUI作为构造函数参数。这样,当任务完成时,它可以调用setCursor方法。

你应该使用SwingWorker来处理这种事情。

编辑:

这里的任务的代码是如何应该是:

public class MySwingWorker extends SwingWorker<Void, Void> { 

    /** 
    * The frame which must have the default cursor set 
    * at the end of the background task 
    */ 
    private JFrame gui; 

    public MySwingWorker(JFrame gui) { 
     this.gui = gui; 
    } 

    // ... 

    @Override 
    protected void done() { 
     // the done method is called in the EDT. 
     // No need for SwingUtilities.invokeLater here 
     gui.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 
    } 
} 
+0

哦,它确实,Task类已经扩展了SwingWorker。我想我明白这是如何工作的,所以在公共类YouDetectTaskStatCl扩展SwingWorker {是否需要成为公共类YouDetectTaskStatCl(JFrame GUI)扩展SwingWorker {? – user585522

+0

我会更新我的答案 –

1

当您创建任务,它传递了某种“完成”方法的接口。完成后让你的任务调用该方法,然后让你的GUI类实现该接口并更改该方法调用的光标。

0

也许你可以尝试让GUI决赛。

final JComponent guiFinal = gui; 
javax.swing.SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
         guiFinal .setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 
        } 
       }); 
相关问题