Java SwingUtilities.invokeLater

发布于 2024-10-18 20:28:01 字数 1074 浏览 11 评论 0原文

.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){
                }
        }
    });

您好,我设计的这个应用程序的最后阶段有点痛苦。

基本上,当用户单击按钮时,我希望光标变成“等待”版本,然后一旦后台进程(task.execute)完成,光标就会恢复正常。

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

不知道该怎么做,所以任何建议都会很好

谢谢:D

.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){
                }
        }
    });

Hi there, having a bit of a pain with the last stage of this application I've designed.

Basically, when the user clicks the button, I'd like it so the cursor becomes a 'waiting' version and then once the background process (task.execute) has completed, the cursor returns to normal.

The task.execute isn't in the same class so I can't just do a straight call of the "gui.setCursor" because it doesn't recognise GUI as the variable.

Not sure what to do so any advice would be great

Thanks :D

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

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

发布评论

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

评论(3

笑着哭最痛 2024-10-25 20:28:01

修改任务的类,以便它将您的 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));
    }
}

Modify the task's class so that it takes your GUI as a constructor argument. This way, when the task is completed, it can invoke the setCursor method.

You should use a SwingWorker for this kind of thing.

EDIT :

Here's how the code of the task should be :

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));
    }
}
轻许诺言 2024-10-25 20:28:01

当您创建任务时,向其传递一个具有某种“已完成”方法的接口。让您的任务在完成时调用该方法,然后让您的 gui 类实现该接口并更改该方法调用上的光标。

When you create your task pass it an interface that has some sort of "completed" method. Make your task call that method when it is finished then have your gui class implement that interface and change the cursor on that method call.

鲸落 2024-10-25 20:28:01

也许你可以尝试将 gui 定为最终版本。

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

Maybe you can try to make gui final.

final JComponent guiFinal = gui;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        guiFinal .setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    }
                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文