Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate button when corresponding action is executed thru hotkey

I have actions coupled with buttons as well as hotkeys. I want to animate(animation similar to the one exhibited during mouse click) the button when its respective action is triggered thru hotkeys. Is that possible?

I am doing as follows:

        btnAdd.setAction(addDataAction);
        panelAdd.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
                .put(KeyStroke.getKeyStroke("ctrl ENTER"), addDataAction);
        panelAdd.getActionMap().put(addDataAction, addDataAction);
like image 723
Ahamed Avatar asked Mar 06 '26 07:03

Ahamed


1 Answers

This does the trick for me (not elegant but it works)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public class Test extends JFrame {

    public static void main(String[] args) {
        Test t = new Test();
        final JButton button = new JButton();
        AbstractAction action = new AbstractAction("Hello World!") {

            @Override
            public void actionPerformed(ActionEvent e) {
                button.getModel().setArmed(true);
                button.getModel().setPressed(true);
                Timer t = new Timer(200, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        button.getModel().setArmed(false);
                        button.getModel().setPressed(false);
                    }
                });
                t.setRepeats(false);
                t.start();
                // Do action stuff
            }
        };
        button.setAction(action);

        JPanel panel = new JPanel();
        panel.add(button);

        panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ctrl ENTER"), action);
        panel.getActionMap().put(action, action);

        t.add(panel);

        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.pack();
        t.setVisible(true);
    }
}

From the Java API docs for ButtonModel:

Pressing the mouse on top of a button makes the model both armed and pressed. As long as the mouse remains down, the model remains pressed, even if the mouse moves outside the button. On the contrary, the model is only armed while the mouse remains pressed within the bounds of the button (it can move in or out of the button, but the model is only armed during the portion of time spent within the button). A button is triggered, and an ActionEvent is fired, when the mouse is released while the model is armed - meaning when it is released over top of the button after the mouse has previously been pressed on that button (and not already released). Upon mouse release, the model becomes unarmed and unpressed.

like image 133
ughzan Avatar answered Mar 07 '26 20:03

ughzan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!