• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Turning off Tab traversal in a JTextArea

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can override the processComponentKeyEvent() method to grab the keypress before it gets put into the document...



-Nate
 
Jain Neeraj
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Arthur, where are your pants? Check under this tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic