e986. moving the focus with the tab key in a jtextarea component.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 22 行

TXT
22
字号
By default, typing TAB in a JTextArea inserts a TAB character. This example demonstrates how to modify the behavior so that typing TAB moves the focus to the next focusable component. 
Rather than try to find the inputmap or keymap with the TAB binding and remove it, it is better to add an overriding key binding that will move the focus. The reason is that the location of the default TAB binding might change in some future version. 

    JTextArea component = new JTextArea();
    
    // Add actions
    component.getActionMap().put(nextFocusAction.getValue(Action.NAME), nextFocusAction);
    component.getActionMap().put(prevFocusAction.getValue(Action.NAME), prevFocusAction);
    
    // The actions
    public Action nextFocusAction = new AbstractAction("Move Focus Forwards") {
        public void actionPerformed(ActionEvent evt) {
            ((Component)evt.getSource()).transferFocus();
        }
    };
    public Action prevFocusAction = new AbstractAction("Move Focus Backwards") {
        public void actionPerformed(ActionEvent evt) {
            ((Component)evt.getSource()).transferFocusBackward();
        }
    };

 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?