This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hello friends, any idea how can i turn off Tab traversal in a JTextArea (means when i hit tab key in JtextArea component, the control should go to the next component instead of adding Tab characters in the TextArea.) Thanks, -N.
This thread helped me solve the tab/jtextarea issue, but the code was not 100% on target. Here was my solution:
This supports both forward (Tab) and backwards (Shift-tab) traversal.
I think there is a better way to do this but I was unable to figure it out. The thinking is as follows:
Events basically get sent to event listeners, in a certain order, and listeners do something (or don't) and consume the event (or don't), and if the event was not consumed, then the next listener in line gets them.
Now, I'm not exactly sure of the naming of these classes, but the ordering of listeners looks something like this for our NoTabTextArea
1. processComponentKeyEvent (in the code above) has first dibs on the key event. 2. sometime later, a focusTraversalKeyEvent (I don't know exactly what it is really called) is supposed to check if the key was a focus-transfer key (tab key or a shift-tab combo for most modern OS's) and tranfer focus accordingly. In the case of JTextArea, either this listener isn't hooked up or it does nothing. 3. The JTextArea's content listener is last in line, gets the key event, and adds a tab to the text.
So the solution above is sort of backwards. An optimal solution would be to reattach #2, that is the focus key listener, so jtextarea acts like every other component (jtextfield, for example). This would also net the benefit of supporting arbitrary traversal keys. Say in the future, Windows 2032 XXXP uses F1 and Shift-F1 for focus traversal. The above solution is hardcoded for tab/shift-tab, whereas the better solution I'm looking for would use whatever Java sniffs out/has defined as the default focus traversal keys, without me having to know them a priori.
So, Swing Gurus -- can you point me in the direction of my fantasy solution? I have tried to figure it out to no avail. The above code works, but it is a hack, and I'd prefer to find the "right way" to do it.