Meaningless Drivel is fun!
The moose likes Swing / AWT / SWT and the fly likes JButton triggered method slowdown error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton triggered method slowdown error" Watch "JButton triggered method slowdown error" New topic

JButton triggered method slowdown error

Ray Muirhead
Ranch Hand

Joined: Jun 11, 2004
Posts: 44

I have a problem with using an inner class as an ActionListener for a JButton to make an animation start and stop. The animation is controlled via integers x and y for location of a circle on the screen, and a boolean crazy that keeps the animation running. Extracts of the relevant code sections are below:

locoButton.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
if (crazy) {
crazy = false;
locoButton.setText("Go loco, little circle!");
else {
crazy = true;
locoButton.setText("Calm Down");


public void goLoco () {
while (crazy) {

x += (int) ((Math.random() * 6) + 1);
y += (int) ((Math.random() * 6) + 1);
x -= (int) ((Math.random() * 6) + 1);
y -= (int) ((Math.random() * 6) + 1);


try {
} catch (Exception ex) {}
x = def; y = def;

goLoco() runs fine when called from main(), but when called by the JButton, it causes the JButton to stay pushed down and the code not do anything. Tested also with a non-repeating loop, the JButton does the same thing and the animation does not display but the circle jitters a little and moves to the endpoint.

I think that it could be becuase the JButton is staying at the bottom of the stack and waiting for the other methods to complete before it will pop back up. I don't know why it is causing the animation to not work though. It is the JButton and not the inner class, as I tried using a non-anonymous inner class as well as the larger class as listeners with the same result.

Is there a way around this or a better way to control animations?

Thank you for your help.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
This is a threading problem. In AWT/Swing applications, there are at least two threads: the main thread, and the AWT thread. The former is what your main method will be called in, the latter is responsible for drawing your components on the screen and reacting to events from the GUI, such as pressing buttons.

That is, when your animation is called from the main method, it is running concurrently to the AWT thread, and everything is fine. When the animation loop is called from the button's listener, though, it is executed inside the AWT thread, so while the loop is running, your program can't paint itself on the screen or react in any way to user input. More specifically, your call to repaint won't have any effect before the loop is exited.

The solution is to leave the AWT thread inside the button's listener. The easiest way to do this is to use a library like Spin - it also comes with a slightly more elaborated explanation of the issue.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch, Ray!

I'm moving this to the Swing / JFC / AWT forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ray Muirhead
Ranch Hand

Joined: Jun 11, 2004
Posts: 44
Cheers Ilja - the Spin documentation looks really helpful.

I agree. Here's the link:
subject: JButton triggered method slowdown error
jQuery in Action, 3rd edition