In a simple calculator app, I use abstract actions to handle the buttons being clicked with the mouse and the respective number pad keys (with key bindings) being pressed. I wanted certain cosmetic changes to occur upon using the keyboard, such as changing the border of JButton number 1 when I press the number 1 key. And changing it back upon release. This all works. Then I went and started pressing the JButtons with my mouse again and realized that the key released action never gets invoked, obviously because I'm not using the keybinding. So my question is, is there a way to invoke the appropriate released abstract action when the mouse release/button raises?
When I discovered this, I initially tried this:
Abstract action(){
set border to this..
code..
code..
code..
set border to this..
}
So no matter if the key or the mouse, it would change. However it doesn't change, or probably goes so fast it's undetectable.
It doesn't make sense to register a mouse listener in this instance. I tried this anyway and I cannot seem to register the abstract action as a mouse listener.
Thanks for your input and ideas.
I register actionlistener:
btnMultiplication.addActionListener( operatorAction );
btnDivision.addActionListener( operatorAction );
btnAddition.addActionListener( operatorAction );
btnSubtraction.addActionListener( operatorAction );
btnSix.addActionListener( numberAction );
btnSeven.addActionListener( numberAction );
btnEight.addActionListener( numberAction );
the *Action's are abstract actions
I use this for keyboard input
im.put( KeyStroke.getKeyStroke( KeyEvent.VK_NUMPAD0, 0, false ), "Number" );
im.put( KeyStroke.getKeyStroke( KeyEvent.VK_NUMPAD0, 0, true ), "Number Released" );
am.put( "Number", numberAction );
am.put( "Number Released", numberActionR );
I used the Number action to change the border of the respective jbutton. Then I use Number Released to change the border again.
Obviously, when I click with the mouse, the border highlights. But the Number Released doesn't invoke. Like I stated, eliminating the released aspect all together and putting the first border change at the start of the abstract action and then the final border change at the end of the abstract action evidently goes so fast you cannot see the border change.