JLabel不会更新,除非某些原因导致该方法挂起 [英] JLabel will not update unless something causes the method to hang

查看:222
本文介绍了JLabel不会更新,除非某些原因导致该方法挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,道歉,但是很难为此重建一个SSCCE,虽然我可以很好地解释这个情况,并得到我的问题。

Firstly, apologies but it would be really hard to reconstruct a SSCCE for this, though I think I can explain the situation considerably well and get my question across,

我的情况是这样的:我有一个JLabel作为一个状态指示器(例如,将显示正在加载...或就绪),我调用 setText 方法在 MouseAdapter 中调用另一个方法来执行实际操作。但是,JLabel文本不会改变,除非我做类似调用 JOptionPane.showMessageDialog()的情况下,文本会更新。

My situation is as thus: I have a JLabel that serves as a status indicator (e.g. that will display "Loading..." or "Ready") and I call the setText method in a MouseAdapter before another method is called to do the actual action. However, the JLabel text never changes, unless I do something like call JOptionPane.showMessageDialog() in which case the text does update.

那么,有没有人有任何建议,如何我可以解决这种情况,而不做类似的东西显示一个消息框(什么是什么)没有理由。

So, does anybody have any suggestions as to how I can resolve this situation without doing something like displaying a message box for (what would be) no reason whatsoever?

提前感谢

推荐答案

确保不要运行任务(您的加载...在EDT(事件分派线程);

Make sure you don't run your task (your "Loading..." procedure) on the EDT (Event Dispatch Thread); if you do so, your GUI won't get updated.

您必须运行您的应用程序代码(除非它非常快,比如小于100毫秒,没有网络访问,没有DB访问等)在一个单独的线程。 SwingWorker (见javadocs)类可能会很方便。

You have to run your application code (unless it's very fast, say less than 100ms, no network access, no DB access, etc) on a separate thread. The SwingWorker (see javadocs) class might come handy for this purpose.

EDT接口侦听器)应该仅包含用于更新GUI,在Swing组件上操作等的代码。您应该在自己的Runnable对象上运行其他任何内容。

The EDT (e.g. code blocks inside user interface listeners) should only contain code for updating the GUI, operating on Swing components, etc. Everything else you should run on its own Runnable object.

-

编辑:回应Andy的评论。这里有一个原始示例(即时写,它可能有拼写错误等等,可能不会按原样运行)如何使用 SwingWorker class

reponse to Andy's comment. Here's a raw example (written on the fly, it might have typos and such and might not run as-is) of how you can use the SwingWorker class

将此项放入您的鼠标监听器事件或任何使任务开始的事件

Put this in your mouse listener event or whatever makes your task start

//--- code up to this point runs on the EDT
SwingWorker<Boolean, Void> sw = new SwingWorker<Boolean, Void>()
{

    @Override
    protected Boolean doInBackground()//This is called when you .execute() the SwingWorker instance
    {//Runs on its own thread, thus not "freezing" the interface
        //let's assume that doMyLongComputation() returns true if OK, false if not OK.
        //(I used Boolean, but doInBackground can return whatever you need, an int, a
        //string, whatever)
        if(doMyLongComputation())
        {
            doSomeExtraStuff();
            return true;
        }
        else
        {
            doSomeExtraAlternativeStuff();
            return false;
        }
    }

    @Override
    protected void done()//this is called after doInBackground() has finished
    {//Runs on the EDT
        //Update your swing components here
        if(this.get())//here we take the return value from doInBackground()
            yourLabel.setText("Done loading!");
        else
            yourLabel.setText("Nuclear meltdown in 1 minute...");
        //progressBar.setIndeterminate(false);//decomment if you need it
        //progressBar.setVisible(false);//decomment if you need it
        myButton.setEnabled(true);
    }
};
//---code under this point runs on the EDT
yourLabel.setText("Loading...");
//progressBar.setIndeterminate(true);//decomment if you need it
//progressBar.setVisible(true);//decomment if you need it
myButton.setEnabled(false);//Prevent the user from clicking again before task is finished
sw.execute();
//---Anything you write under here runs on the EDT concurrently to you task, which has now been launched

这篇关于JLabel不会更新,除非某些原因导致该方法挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆