Press shift space or shift F2 to move the focus to the previous focusable component.
import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
public class Main {
public static void main(String[] argv) {
JButton component = new JButton();
PrevFocusAction prevFocusAction = new PrevFocusAction();
component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("shift F2"),
prevFocusAction.getValue(Action.NAME));
component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("shift SPACE"),
prevFocusAction.getValue(Action.NAME));
component.getActionMap().put(prevFocusAction.getValue(Action.NAME), prevFocusAction);
}
}
class PrevFocusAction extends AbstractAction {
PrevFocusAction() {
super("Move Focus Backwards");
}
public void actionPerformed(ActionEvent evt) {
((Component) evt.getSource()).transferFocusBackward();
}
}
Related examples in the same category