How can I make a swing JButton repeat its action when it is held down?

I am creating an touch screen application using Swing and have a request to change one of buttons so that it will behave like a keyboard when the button is held down. (First of all, I am not sure that the touch screen will allow the user to "hold down" the button, but pretend that they can for now)

I was going to go down the path of starting a loop when mousePressed was called and then ending the loop when mouseReleased was called. This will involve starting a thread and having to deal with synchronization as well as invokeLater() to get events back on the EventQueue.

Is there a very simple way to do what I want? I hope I am just not seeing the API to do it.

Answers


javax.swing.Timer is your friend. And here's an article with some more info.


Need Your Help

mysql - what does a : before a value in sql mean?

mysql sql prepared-statement

I'm a noob with sql and trying to figure my way through zencart. There's a bit in the code where it prepares a SELECT statement and amongst that there is the following line:

SonarQube Lombok Getter recognition

sonarqube lombok sonarqube-4.5

I am using SonarQube 4.5.4 with Java plugin 3.1. As I know this sonar-java version supports Lombok partially (Getter and Setter annotations) starting from 2.8.