aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Turning off Tab traversal in a JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Turning off Tab traversal in a JTextArea" Watch "Turning off Tab traversal in a JTextArea" New topic
Author

Turning off Tab traversal in a JTextArea

Jain Neeraj
Ranch Hand

Joined: Aug 09, 2001
Posts: 35
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.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You can override the processComponentKeyEvent() method to grab the keypress before it gets put into the document...



-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Jain Neeraj
Ranch Hand

Joined: Aug 09, 2001
Posts: 35
Thanks a lot for the quick answer, btw i found some other way round like...
JTextArea jTextArea1 = new JTextArea()
{
public boolean isManagingFocus()
{
return false;
}
};
Thanks,
N.
Idan Gazit
Greenhorn

Joined: Apr 08, 2004
Posts: 5
Hey hey,

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.

Idan
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
Wouldn't the proper way to do this be to:


I believe isManagingFocus is deprecated.

In answer to Idan's question, if you want to change the way that focus traversal is handled, you need to write a new FocusTraversalPolicy and set this as the policy for the component.

Regards,
Ken
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Turning off Tab traversal in a JTextArea