论坛首页 入门技术论坛

Swing Timer

浏览 1383 次
锁定老帖子 主题:Swing Timer
该帖已经被评为新手帖
作者 正文
   发表时间:2010-11-10  
Lab mate came by and talked about a feature he needs to update several textfield periodically on a panel. He is monitoring some resources and will generate a file for display every second, so his UI only needs to read the one-line file once a second and update the textfields. So we got a chance to use swing's Timer haha.
				Timer timer = new Timer(1000, new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						System.out.println("repaint " + Thread.currentThread().getName());
			
						Random rand = new Random();
						int base = rand.nextInt(100);
						textField_1.setText("" + base);
						textField_2.setText("" + (base+1));
						textField_3.setText("" + (base+2));
						textField_4.setText("" + (base+3));
					}});
				timer.start();

something like this works. Intrinsically, there is a thread dedicated for running this actionPerformed() every second. This thread actually fired actionEvents to our listener.
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics