use*_*522 2 java swing cursor invokelater swingutilities
.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){
}
}
});
Run Code Online (Sandbox Code Playgroud)
嗨,我设计的这个应用程序的最后阶段有点痛苦.
基本上,当用户点击按钮时,我会喜欢它,所以光标变成'等待'版本,然后一旦后台进程(task.execute)完成,光标就会恢复正常.
task.execute不在同一个类中,因此我不能直接调用"gui.setCursor",因为它不能将GUI识别为变量.
不知道该怎么做,所以任何建议都会很棒
感谢:D
修改任务的类,使其将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));
}
}
Run Code Online (Sandbox Code Playgroud)