简体   繁体   English

标签文本在Java中未更改

[英]The text of the Label not changing in Java

Im creating a UI for my project and that involves JLabel (javax.swing) which needs to update the text (of the Label) everytime that task is done. 我为我的项目创建了一个UI,该UI涉及JLabel(javax.swing),该JLabel需要在每次完成任务时更新(标签的)文本。

I use label.setText to change/update the text inside (like the one shown below.) However, most of the time, the label doesnt change the text. 我使用label.setText更改/更新其中的文本(如下图所示。)但是,在大多数情况下,标签不会更改文本。 I tried to use label.UpdateUI() hoping that it will solve the problem, but it doesnt. 我试图使用label.UpdateUI()希望它可以解决问题,但事实并非如此。

taskLabel.setText(msg);

Any ideas? 有任何想法吗?

if (gatherSamplesValue)
{
      if (SourceACheckBox)
      {
           try 
           { 
                Thread setLabelText1 = new Thread(){
                    public void run(){
                        taskLabel.setText(msg);

                    taskLabel.validate();
                }
             };
             msg = "Task : Gathering URLs from Phish Tank. DateTime: " + Main.getCurrentDate();
             setLabelText1.start();
             SourceA.sourceAMain();
             msg="Done with the task : " + Main.getCurrentDate();
             Thread setLabelText2 = new Thread(){
                public void run(){
                    taskLabel.setText(msg);
                    taskLabel.validate();
                }
             };
             setLabelText2.start();
      } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
      }
 }

 if (SourceBCheckBox)
 {
  .
  .
 }

 if (SourceCCheckBox)
 {
  .
  .
 }

} }

Here's the code, imagine that I nee to put the same commmand inside Source A and B. The Thread inside that is not working. 这是代码,假设我需要将相同的命令放在Source A和B中。其中的Thread无法正常工作。

..needs to update the text (of the Label) everytime that task is done. ..需要在每次任务完成时更新(标签的)文本。

  • you have issue with Concurency in Swing 您对Swing中的Concurency有问题

  • all updates to the Swing GUI must be done on EDT, even Whatever.setText() is declared as thread safe, but usage of Thread.sleep(int) from Swing Listener by default freeze Swing GUI and any changes aren't updated on the screen Swing GUI的所有更新都必须在EDT上完成,即使Whatever.setText()被声明为线程安全的,但默认情况下, Swing ListenerThread.sleep(int)使用也会冻结Swing GUI,并且任何更改都不会在屏幕

  • hack will be put taskLabel.setText(msg); hack将被放置taskLabel.setText(msg); into invokeLater() , then output to the Swing GUI should be done on EDT 进入invokeLater() ,然后应在EDT上输出到Swing GUI

  • see and to use SwingWorker as one of proper of ways, where methods process , publish and done notified EDT 看到并使用SwingWorker作为适当的方法之一,其中方法processpublishdone通知EDT

After you add the text to JLabel you should add that to the panel like this. 在将文本添加到JLabel之后,应将文本添加到面板中,如下所示。 label.setText("something"); label.setText(“ something”); panel.add(label) panel.add(标签)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM