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.
A JToggleButton is a two-state button. The two states are selected and unselected. The JRadioButton and JCheckBox classes are subclasses of this class. When the user presses the toggle button, it toggles between being pressed or unpressed. JToggleButton is used to select a choice from a list of possible choices.
A JButton object draws itself and processes mouse, keyboard, and focus events on its own. You only hear from the JButton when the user triggers it by clicking on it or pressing the space bar while the button has the input focus. When this happens, the JButton object creates an event object belonging to the class java.
The actionPerformed method is used when a button is clicked normally. If you want to do some fancy interaction with the button you can also use other events like mousePressed in the MouseListener.
javax.swing.Timer is your friend. And here's an article with some more info.
I would do it like this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With