Example usage for javax.swing JTextArea getInputMap

List of usage examples for javax.swing JTextArea getInputMap

Introduction

In this page you can find the example usage for javax.swing JTextArea getInputMap.

Prototype

public final InputMap getInputMap() 

Source Link

Document

Returns the InputMap that is used when the component has focus.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextArea comp = new JTextArea();
    String actionName = "Lowercase";

    JFrame f = new JFrame();
    f.add(new JScrollPane(comp));
    f.setSize(300, 300);// w w w.j  a  v  a  2  s. com
    f.setVisible(true);
    comp.getInputMap().put(KeyStroke.getKeyStroke("F2"), actionName);

    comp.getActionMap().put(actionName, new TextAction(actionName) {
        public void actionPerformed(ActionEvent evt) {
            JTextComponent comp = getTextComponent(evt);

            if (comp.getSelectionStart() == comp.getSelectionEnd()) {
                if (comp.getCaretPosition() < comp.getDocument().getLength()) {
                    try {
                        int pos = comp.getCaretPosition();
                        Document doc = comp.getDocument();
                        String str = doc.getText(pos, 1).toLowerCase();

                        doc.remove(pos, 1);
                        doc.insertString(pos, str, null);
                        comp.moveCaretPosition(pos + 1);
                    } catch (Exception e) {
                        System.out.println();
                    }
                }
            } else {
                int s = comp.getSelectionStart();
                int e = comp.getSelectionEnd();

                comp.replaceSelection(comp.getSelectedText().toLowerCase());
                comp.select(s, e);
            }
        }
    });
}

From source file:edu.ku.brc.ui.UIHelper.java

public static JTextArea createTextArea() {
    final JTextArea text = new JTextArea();
    setControlSize(text);/*from   ww w  .  j  a  v a  2s .c o m*/

    // Enable being able to TAB out of TextArea
    text.getInputMap().put(KeyStroke.getKeyStroke("TAB"), "none");
    text.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent event) {
            if (event.getKeyCode() == VK_TAB) {
                if (event.isShiftDown()) {
                    text.transferFocusBackward();
                } else {
                    text.transferFocus();
                }
            }
        }
    });
    return text;
}