Thursday, February 18, 2010

JTextArea vs. Tab focus cycle

When inside a JTextArea, the Tab key inserts a tab character instead of going to the next field. The reason is obvious - but in several situations it is not desirable, in particular when it would not make sense to insert tab characters, for example in a text area meant for editing keywords.

Lets just spill the solution right away:
JTextArea text = new JTextArea();
text.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
text.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);

There is also a built-in solution, which is that Ctrl-[Shift-]Tab still works. Actually, those keys always work. Those keys are also the solution when you suddenly find yourself focus-stuck within a JTable. I first thought that a JTable constituted a a "focus cycle root", but it doesn't - it is just the same effect as with the JTextArea problem here outlined (and can be "fixed" the same way).

However, Ctrl-Tag does not quite give the feeling one typically is after, and in particular not when one use the JTextArea more as a solution to text that typically is less than a line big, but can become multiline. Such a field thus looks like a JTextField, but the Tab character suddenly have a completely different meaning.

I Googled a lot, and there are lots of solutions (or rather hacks), but didn't find the above solution, which seems to be the most straightforward after Java 1.5. The huge heap of questions and solutions should, IMHO, give a hint to the Swing team that a specific method for a JTextArea to not suck up Tab events should be made available.

The null means that it will take the focus keys from its parent.


  1. Thanks, that was just what I needed. Very elegant solution!

  2. Hiện nay có nhiều cách trang điểm và địa điểm dạy cách trang điểm tốt như dịch vụ trang điểm cô dâu tại hcm, dạy trang điểm tại nhà thì nên sử dụng bộ mỹ phẩm trang điểm cơ bản. Khi trang điểm thì bạn nên sử dụng các sản phẩm trang điểm như mỹ phẩm trang điểm shiseido, mỹ phẩm trang điểm maybeline và khó nhất trong trang điểm là cách trang điểm cho mắt một mí, hy vọng sẽ giúp bạn có được cách trang điểm hiệu quả và tốt nhất.