I have this code here to create a key binding:
KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
getInputMap(WHEN_IN_FOCUSED_WINDOW).put(k, k.toString());
getActionMap().put(k.toString(), new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
//put action here
}
});
The problem is I have 8 of these across my program. Each of these creates a separate class file to hold the abstract class. How would I rewrite this, if possible, to limit the amount of classes created. (I have searched for this, but reduce abstract classes doesn’t come up with anything useful)
In addition to extension, suggested here by @EdC, you can use composition, as shown in this example in which the
Actionuses parameters specified to the constructor of an enclosingJButton. Also, oneActioncan forward itsActionEventto anotherAction, as shown in thisKeyPadPanel.