XDEVTextField的值未实时更新。

huangapple go评论77阅读模式
英文:

XDEVTextField's value isn't updated to real time

问题

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

问题是txtTime的值根本没有持续更新,该值仅在线程启动时设置一次。
我正在使用Rapidclipse 3.1.1。我曾经使用Java Swing的JLabel在Netbeans上制作过一个类似的数字时钟。我不知道问题可能是什么。我怀疑并检查了txtTime元素的所有属性,但似乎没有任何原因。任何建议或解决方案将不胜感激。

英文:

I have an XDEVTextField that needs to update real time by every second. I tried the answers proposed in this question, including using Executor and Swing Timer. I also tried this way.<br>
Below is my code: <br>

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

    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(&quot;dd/MM/yyyy HH:mm:ss&quot;);
	                final Date date = cal.getTime();
	                this.timeString = formatter.format(date);
	                System.out.println(this.timeString);
	                this.txtTime.setValue(this.timeString);
	                Thread.sleep(500); &lt;-- 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<br>XDEVTextField的值未实时更新。<br><br> while the System.out.println(dateandtime.format(date)); can still print out the real time to the console, like this:<br>XDEVTextField的值未实时更新。
<br><br>I'm using Rapidclipse 3.1.1. I made a similar digital clock like this one using Java Swing's JLabel on Netbeans. 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. Any suggestion or solution would be appreciated.

答案1

得分: 1

你只能使用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();
    }
}
英文:

you may only access an UI using the access() method, which locks the session to prevent conflicts.

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

huangapple
  • 本文由 发表于 2020年10月12日 14:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64312768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定