File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JButton triggered method slowdown error

 
Ray Muirhead
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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");
gui.goLoco();
}
}

});

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);

drawPanel.repaint();

try {
Thread.sleep(45);
} 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
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Ray!

I'm moving this to the Swing / JFC / AWT forum...
 
Ray Muirhead
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Ilja - the Spin documentation looks really helpful.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic