JButton long press event

夙愿已清 提交于 2019-12-13 07:07:59

问题


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.


回答1:


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'.




回答2:


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");
        }
    });


来源:https://stackoverflow.com/questions/23872483/jbutton-long-press-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!