Java XDEVTextField';s值为';t实时更新
我有一个XDEVTextField,需要每秒实时更新一次。我尝试了问题中提出的答案,包括使用Executor和Swing Timer。我也试过了。Java XDEVTextField';s值为';t实时更新,java,date,time,timer,rapidclipse,Java,Date,Time,Timer,Rapidclipse,我有一个XDEVTextField,需要每秒实时更新一次。我尝试了问题中提出的答案,包括使用Executor和Swing Timer。我也试过了。 下面是我的代码: public class FirstView extends XdevView implements Runnable { Thread t = null; String timeString = ""; public FirstView() { super();
下面是我的代码:
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();
}
}
public类FirstView扩展XdevView实现可运行{
线程t=null;
字符串timeString=“”;
公共第一视图(){
超级();
this.initUI();
this.t=新线程(this);
这个.t.start();
}
@凌驾
公开募捐{
//TODO自动生成的方法存根
试一试{
while(true){
最终日历cal=Calendar.getInstance();
最终SimpleDataFormat格式化程序=新SimpleDataFormat(“dd/MM/yyyy HH:MM:ss”);
最终日期=cal.getTime();
this.timeString=formatter.format(日期);
System.out.println(this.timeString);
this.txtTime.setValue(this.timeString);
sleep(500);您只能使用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();
}
}