Moving the Focus with the TAB Key in a JTextArea Component
import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] argv) throws Exception {
JTextArea component = new JTextArea();
NextFocusAction nextFocusAction = new NextFocusAction();
PrevFocusAction prevFocusAction = new PrevFocusAction();
component.getActionMap().put(nextFocusAction.getValue(Action.NAME), nextFocusAction);
component.getActionMap().put(prevFocusAction.getValue(Action.NAME), prevFocusAction);
}
}
class NextFocusAction extends AbstractAction{
public NextFocusAction(){
super("Move Focus Forwards");
}
public void actionPerformed(ActionEvent evt) {
((Component) evt.getSource()).transferFocus();
}
}
class PrevFocusAction extends AbstractAction {
public PrevFocusAction(){
super("Move Focus Backwards");
}
public void actionPerformed(ActionEvent evt) {
((Component) evt.getSource()).transferFocusBackward();
}
}
Related examples in the same category