简体   繁体   English

XDEVTextField 的值未实时更新

[英]XDEVTextField's value isn't updated to real time

I have an XDEVTextField that needs to update real time by every second.我有一个 XDEVTextField 需要每秒实时更新。 I tried the answers proposed in this question, including using Executor and Swing Timer.我尝试了这个问题中提出的答案,包括使用 Executor 和 Swing Timer。 I also tried this way .我也试过这种方法
Below is my code:下面是我的代码:

public class FirstView extends XdevView implements Runnable {
    Thread t = null;
    String timeString = "";

    public FirstView() {
        super();
        this.initUI();
        this.t = new Thread(this);
        this.t.start();
    }

    @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                 while (true) {
                    final Calendar cal = Calendar.getInstance();
                    final SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                    final Date date = cal.getTime();
                    this.timeString = formatter.format(date);
                    System.out.println(this.timeString);
                    this.txtTime.setValue(this.timeString);
                    Thread.sleep(500); <-- this should be changed to 1000 but I forgot to
                 } 
              }
              catch (final Exception e) {
                  e.printStackTrace();
              }
        }

The problem is the txtTime 's value isn't updated continuously at all, the value was only set once at the point the Thread started问题是txtTime的值根本没有连续更新,该值仅在线程启动时设置一次
我的 GUI 预览

while the System.out.println(dateandtime.format(date));System.out.println(dateandtime.format(date)); can still print out the real time to the console, like this:仍然可以将实时时间打印到控制台,如下所示:
控制台

I'm using Rapidclipse 3.1.1.我正在使用 Rapidclipse 3.1.1。 I made a similar digital clock like this one using Java Swing's JLabel on Netbeans.我在 Netbeans 上使用 Java Swing 的 JLabel 制作了一个类似的数字时钟。 I have no clue what can be the problem here.我不知道这里有什么问题。 I suspected and checked all the Properties of the txtTime element but nothing seems to be the cause to this.我怀疑并检查了txtTime元素的所有属性,但似乎没有任何原因。 Any suggestion or solution would be appreciated.任何建议或解决方案将不胜感激。

you may only access an UI using the access() method, which locks the session to prevent conflicts.您只能使用 access() 方法访问 UI,该方法会锁定会话以防止冲突。

@Override
    public void run() {
        try {
            while (true) {
                this.time = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date());
                System.out.println(this.time);
                UI.getCurrent().access(()->this.button.setCaption(this.time));
                this.t.sleep(1000);
                
            }
        } catch (final InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

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

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