Swing Timer

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.

你可能感兴趣的:(thread,UI,swing)