Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Java JButton visually depress with touch screen?

I've got a simple Swing GUI with JButtons being run on a Surface tablet with a touchscreen. The buttons have ActionListeners. When these buttons are clicked from a mouse they visually depress correctly. However when they are tapped on the touchscreen they remain visually the same but still fire off an actionPerformed(). If they are double tapped then they visually depress correctly but fire off 2 actionPerformed()s.

Is there a way to get this button animation change to take place when the button is pressed, rather than clicked? I've tested it and I could use a MouseListener and put all my logic in mouseClicked() but it's not very elegant to ask touchscreen users to double tap a button.

like image 933
Stuart Lacy Avatar asked Sep 08 '14 13:09

Stuart Lacy


People also ask

What is swing button?

Swing defines four types of buttons: JButton, JToggleButton, JCheckBox, and JRadioButton. All are subclasses of the AbstractButton class, which extends JComponent. Thus, all buttons share a set of common traits. AbstractButton contains many methods that allow you to control the behavior of buttons.

What is Java Swing button?

August 23, 2022. A button is a Swing component in Java that is usually used to register some action from a user. The action comes in the form of a button being clicked. To use a button in an application or as part of a graphical user interface (GUI), developers need to create an instance of the JButton class.

Which method is used to disable a JButton?

Then use jQuery method attr('disabled', true) to disable that button with class ui-button.


1 Answers

The problem you have is that whereas a mouse click is a compound event, a touch on the screen is not. So there's no way that it can go down at the first event and up again at the second.

But what you can do, when you receive a touch event, is change the visual state of the button so it looks depressed (using setPressed(true)), then set a timer for 100ms or so, and set the state back to normal when the timer expires (using setPressed(false)).

Careful with the timer expiration: you need the setPressed(false) to happen on the UI thread. So you'll need to use SwingUtilities.invokeLater() as a wrapper round the second call to setPressed. Or, alternatively, use a javax.swing.Timer as the means of queueing the second call; it takes a delay and an Action, and the Action gets performed on the UI thread.

like image 129
chiastic-security Avatar answered Sep 17 '22 20:09

chiastic-security