How can I modify the behavior of the tab key in a JTextArea?

前端 未结 4 1921
猫巷女王i
猫巷女王i 2020-12-09 17:57

I\'m creating a form in Java Swing, and one of the fields is a JTextArea. When I use the Tab key on all other fields, it gives the focus to the next

4条回答
  •  一生所求
    2020-12-09 18:31

    you could use the "NextWidget.grabFocus()" method within keylistener of JTextArea when key "tab" is pressed

    With this latter approach the tab character will still get inserted into the JTextArea before the focus is shifted away. If you dont want that behavior you can create a subclass of JTextArea whose isManagingFocus() method always returns false, instead of true. For example:

    import javax.swing.*;
    
    public class NoTabTextArea extends JTextArea {
        public boolean isManagingFocus() {
            return false;
        }
    }
    

    An instance of NoTabTextArea can be used exactly like a JTextArea, except that the tab key will cause the focus to shift away from it without a tab character being inserted.

提交回复
热议问题