11

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.

Yogendra
  • 1,728
  • 16
  • 28
Stuart Lacy
  • 1,963
  • 2
  • 18
  • 30
  • 1. [override events from ButtonModel](http://stackoverflow.com/a/5755124/714968) for testing purposes, 2. no idea question missing important details about, here are a few users with touch_screens, 3. important is used L&F, 4. voting to close as too broad – mKorbel Sep 08 '14 at 13:33
  • 5
    This is a perfectly clear, specific, and well written question. It got 10 upvotes in its first half an hour after posting. There is *much* too much knee-jerk closure of good questions round here. – chiastic-security Sep 08 '14 at 13:40

2 Answers2

5

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.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
-1

I use mouse listener but I implement my logic in the moussePressed method and it this works perfectly in IBM touch screens

class BtnNumbMl implements MouseListener {

    public void mousePressed(MouseEvent e) {
        **//your logic goes here**
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

}

have a good day

johan
  • 518
  • 6
  • 18
  • you got test this you are talking in void. we are talking about the mouseClicked method it is not working correctly in touch screens. in another hand mousePressed works as expected. – Boufouss Mohamed Feb 03 '23 at 14:46
  • I don't understand what you're saying "you got test this you are talking in void.". Nobody is saying mouseClicked doesn't work. They're saying tapping a JButton causes the action to fire, but the button never appears pressed. How does your answer solve that? – matt Feb 03 '23 at 15:45
  • Your answer just makes no sense. You have a bunch of stuff that is included but it is unknown lbl1, lbl2? flag? btnValidate? My point is just that you should explain your answer as to why or how it works. – matt Feb 03 '23 at 15:51
  • Ok thank you sir, I am sorry about this. That is just my logic do not look at it at all. Focus about mouseClicked is not working in touch screens and mousePressed responds to user actions as expected. – Boufouss Mohamed Feb 04 '23 at 09:04