2

I am developing Java desktop application in NetBeans platform. I have several JFrames and within these frames I have several JButtons.

My application will be run on touch panels like industrial PCs, Linux based panel PCs etc. So I will need to use long press event of a button.

How can I handle long press event of JButton? Click event is OK but I could not find any references or samples about long press/long click.

user1803551
  • 12,965
  • 5
  • 47
  • 74
Fer
  • 1,962
  • 7
  • 29
  • 58
  • 3
    Swing has no standard support for long press event. But you can implement it. Start timer when user press the button and if no mouseReleased/mouseDragged event is fired you can trigger your action – Sergiy Medvynskyy May 26 '14 at 14:43
  • How can I handle long press event of JButton? - by using events from ButtonModel (by adding the ChangeListener) – mKorbel May 26 '14 at 15:54
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 27 '14 at 03:49

2 Answers2

3

This code worked for me.

abstract class MouseCustomAdapter extends MouseAdapter {
    private long mousePressedTime;
    private long delay = 1000;
    private Timer flashTimer;
    private Color originalForegroungColor;

    public MouseCustomAdapter() {}
    public MouseCustomAdapter(long delay) {
        this.delay = delay;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        mousePressedTime = e.getWhen();
        if(flashTimer != null)
            flashTimer.cancel();
        flashTimer = new Timer("flash timer");
        flashTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                originalForegroungColor = e.getComponent().getForeground();
                e.getComponent().setForeground(Color.LIGHT_GRAY);
            }
        }, delay);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        flashTimer.cancel();
        e.getComponent().setForeground(originalForegroungColor);
        if(e.getWhen() - mousePressedTime > delay)
            longActionPerformed(e);
        else
            shortActionPerformed(e);
    }

    public abstract void shortActionPerformed(MouseEvent e);
    public abstract void longActionPerformed(MouseEvent e);
}

Extends the adapter implementing shortActionPerformed and longActionPerformed. Es:

    menuButton.addMouseListener(new MouseCustomAdapter() {
        @Override
        public void shortActionPerformed(MouseEvent e) {
            System.out.prinln("Pressed short");
        }

        @Override
        public void longActionPerformed(MouseEvent e) {
            System.out.prinln("Pressed long");
        }
    });
0

If you decided for your implementation to use JButton, you should be aware that usually you don't use "click events" with them (although you, in theory, can use some sort of MouseListener combo to achieve this) - all AbstractButton subclasses have an ActionListener queue handling the default platform event of activating the button. You should thus focus on Actions instead of 'clicks'

http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#abstractbutton

http://docs.oracle.com/javase/7/docs/api/javax/swing/Action.html#buttonActions

If you're sure you want to monitor for long press events on JButton objects anyway, add a timer to the ActionListener, e.g. by means of System.currentTimeMillis(), to check the time difference between actions and/or use MouseListener (all java.awt.Component subclasses has addMouseListener() defined) with mousePressed/mouseReleased event time measurement to get the time delta so that you can detect the length of the 'press'.

  • I implemented mousePressed/mouseReleased events and detect the long press by swing timer. – Fer May 27 '14 at 08:17