The recommended way to create motion in Swing components is by using a Timer. You are advised not to use Threads (if you know that they are) due to conflict issues
![[Java/Java GUI/_resources/image.png]]
```java
package countframe;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
/**
*
* @author ajb
*/
public class CountFrame extends JFrame implements ActionListener{
JLabel lblCount = new JLabel("0");
JButton btnStart = new JButton("Start");
Timer timer = new Timer(1000, this);
int count = 0;
CountFrame() {
Container vert = Box.createVerticalBox();
vert.add(lblCount);
vert.add(btnStart);
btnStart.addActionListener(this);
this.add(vert);
this.setSize(200,100);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new CountFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnStart) {
if(timer.isRunning()) {
timer.stop();
btnStart.setText("Start");
} else {
timer.start();
btnStart.setText("Stop");
}
}
else if (e.getSource() == timer) {
count++;
lblCount.setText(""+count);
}
}
}
```
## Timer Exercise
1. Extend the simple counter application. Add a reset button.