tags:

views:

578

answers:

3

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.

+8  A: 

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

ykaganovich
+5  A: 

I would do it like this:

  • Listen to mousePressed and schedule a java.util.Timer to be launched at a later time.
  • The Timer does the action and set itself to schedule again.
  • Listen to mouseReleased to cancel the Timer.
ckarmann
A: 

I went with the java.swing.Timer since it will automatically post back to the Swing EventQueue and that is what I am looking for. Thanks for the help.

Tony Eichelberger