Moving the Focus with the TAB Key in a JTextArea Component - Java Swing

Java examples for Swing:JTextArea

Description

Moving the Focus with the TAB Key in a JTextArea Component

Demo Code


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[] args) throws Exception {
    Action nextFocusAction = new AbstractAction("Move Focus Forwards") {
      public void actionPerformed(ActionEvent evt) {
        ((Component) evt.getSource()).transferFocus();
      }//from   w w  w . j a v a2 s.  co  m
    };
    Action prevFocusAction = new AbstractAction("Move Focus Backwards") {
      public void actionPerformed(ActionEvent evt) {
        ((Component) evt.getSource()).transferFocusBackward();
      }
    };
    JTextArea component = new JTextArea();

    // Add actions
    component.getActionMap().put(nextFocusAction.getValue(Action.NAME),
        nextFocusAction);
    component.getActionMap().put(prevFocusAction.getValue(Action.NAME),
        prevFocusAction);
  }
}

Related Tutorials