Board index » jbuilder » Swap TAB and Ctrl-TAB for JTextArea

Swap TAB and Ctrl-TAB for JTextArea


2005-05-16 10:33:01 PM
jbuilder0
Hi,
I would like to swap the behavior of the TAB function for a JTextArea. I
constantly use TAB to go to next focus and always get caught in the text
area.
I might be bad at finding the simplest thing but I have searched the
web, my java books, jbuilder help, ... but can't find any help on this
subject. (I found some questions on Google news but no replies).
Magnus
 
 

Re:Swap TAB and Ctrl-TAB for JTextArea

Magnus wrote:
Quote
Hi,

I would like to swap the behavior of the TAB function for a JTextArea. I
constantly use TAB to go to next focus and always get caught in the text
area.

I might be bad at finding the simplest thing but I have searched the
web, my java books, jbuilder help, ... but can't find any help on this
subject. (I found some questions on Google news but no replies).

Magnus
Doing a Google search on "JTextArea" and "tab" yields many solutions
from the ridiculously simple:
www.devx.com/tips/Tip/13866
To the insanely complex:
forums.java.sun.com/thread.jspa
--
Regards,
Lori Olson [TeamB]
------------
Save yourself, and everyone else, some time and search the
newsgroups and the FAQ-O-Matic before posting your next
question.
Google Advanced Newsgroup Search
www.google.ca/advanced_group_search
Other Newsgroup Searches:
www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
www.visi.com/~gyles19/fom-serve/cache/1.html
 

Re:Swap TAB and Ctrl-TAB for JTextArea

Thanks Lori,
I will go for the simple:
textArea.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent ke){
if(ke.getKeyCode() == KeyEvent.VK_TAB)
{
ke.consume();
[nextComponent].requestFocus();
}}});
It seems to work pretty good.
I also wanted to implement SHIFT-TAB but couldn't find any
KeyEvent.[SHIFT-TAB] so I went to:
java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html
Now, this example didn't even read tabs...
I will investigate later,
Thanks again.
Lori M Olson [TeamB] wrote:
Quote
Magnus wrote:

>Hi,
>
>I would like to swap the behavior of the TAB function for a JTextArea.
>I constantly use TAB to go to next focus and always get caught in the
>text area.
>
>I might be bad at finding the simplest thing but I have searched the
>web, my java books, jbuilder help, ... but can't find any help on this
>subject. (I found some questions on Google news but no replies).
>
>Magnus


Doing a Google search on "JTextArea" and "tab" yields many solutions
from the ridiculously simple:

www.devx.com/tips/Tip/13866

To the insanely complex:

forums.java.sun.com/thread.jspa

 

{smallsort}

Re:Swap TAB and Ctrl-TAB for JTextArea

On 5/16/2005 at 3:32:48 PM, Magnus wrote:
Quote
textArea.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent ke){
if(ke.getKeyCode() == KeyEvent.VK_TAB)
{
ke.consume();
[nextComponent].requestFocus();
}}});
It looks like you are just trying to customize the tab order. If that
is the case, you should look at java.awt.FocusTraversalPolicy. For more
information on Swing/AWT focus handling, see the trail "How to Use the
Focus Subsystem" in the Java Tutorial:
java.sun.com/docs/books/tutorial/uiswing/misc/focus.html
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Swap TAB and Ctrl-TAB for JTextArea

No. I want to change the behavior of TAB for JTextArea.
When I have a panel with multiple text fields I use TAB to
go to the next field. However, as soon as I entered text in
a JTextArea I inserted a TAB instead of going to the next field.
I can't see why the implementation of TAB behavior should differ
for one component like that. In the MS world CTRL-TAB insert a tab
and I guess that that is what most user is expecting even for Java
developed applications.
Thanks for the input on traversal though - that is something I will
use in other places.
John McGrath [TeamB] wrote:
Quote
On 5/16/2005 at 3:32:48 PM, Magnus wrote:


>textArea.addKeyListener(new KeyAdapter(){
>public void keyPressed(KeyEvent ke){
>if(ke.getKeyCode() == KeyEvent.VK_TAB)
>{
>ke.consume();
>[nextComponent].requestFocus();
>}}});


It looks like you are just trying to customize the tab order. If that
is the case, you should look at java.awt.FocusTraversalPolicy. For more
information on Swing/AWT focus handling, see the trail "How to Use the
Focus Subsystem" in the Java Tutorial:

java.sun.com/docs/books/tutorial/uiswing/misc/focus.html

 

Re:Swap TAB and Ctrl-TAB for JTextArea

On 5/20/2005 at 10:45:31 AM, Magnus wrote:
Quote
When I have a panel with multiple text fields I use TAB to
go to the next field. However, as soon as I entered text in
a JTextArea I inserted a TAB instead of going to the next field.
The TAB key is normally a focus traversal key, but the text for a
JTextArea can contain tabs, so JTextArea changes it to insert a tab
character.
Quote
I can't see why the implementation of TAB behavior should differ
for one component like that.
The TAB key has been used both for focus traversal and for inserting a TAB
character in text editing applications. The current behavior seems like a
reasonable compromise. And if you do not like it, you can change it.
Quote
In the MS world CTRL-TAB insert a tab
It does not do that consistently. I have found many Microsoft
applications on my system in which that does not work.
Are you aware of any official MS specification that says that the TAB key
should work that way?
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Swap TAB and Ctrl-TAB for JTextArea

Quote
The TAB key is normally a focus traversal key, but the text for a
JTextArea can contain tabs, so JTextArea changes it to insert a tab
character.
I noticed.
Quote
>I can't see why the implementation of TAB behavior should differ
>for one component like that.
Now, why is it that a text entry that can work with more than one row
(i.e. text area) insert a TAB but not a text entry for one row (even
though it can be very long)? I don't know what criteria was behind the
TAB decision but I would probably stick with consistency (but that is
just me).
Quote
The TAB key has been used both for focus traversal and for inserting a TAB
character in text editing applications. The current behavior seems like a
reasonable compromise. And if you do not like it, you can change it.
True and I did.
Quote
It does not do that consistently. I have found many Microsoft
applications on my system in which that does not work.
I have found that to but it is not common.
Quote
Are you aware of any official MS specification that says that the TAB key
should work that way?
No.
All browsers I use (explorer, netscape and firefox) also use tab to
traverse.
 

Re:Swap TAB and Ctrl-TAB for JTextArea

On 5/24/2005 at 9:19:58 AM, Magnus wrote:
Quote
Now, why is it that a text entry that can work with more than one row
(i.e. text area) insert a TAB but not a text entry for one row (even
though it can be very long)? I don't know what criteria was behind the
TAB decision but I would probably stick with consistency (but that is
just me).
JTextField allows the user to enter normal characters, while JTextArea
allows the user to insert basic formatting information - in other words,
control characters. So to me, it seems inconsistent to allow TABs, but
not allow newlines.
I also think it is quite rare to actually need to insert TAB characters
into a text field. I searched through many applications on my system,
and while I found a few that would insert a TAB on Control-TAB, I found
none where it actually made sense to do so.
So I do not think it makes sense for this to be the default behavior for
a JTextField. In the rare cases where it is useful, it can be added.
Quote
>It does not do that consistently. I have found many Microsoft
>applications on my system in which that does not work.

I have found that to but it is not common.
On the applications on my system, it is *extremely* common. I have looked
at many applications (Microsoft Word, Excel, Outlook, Notepad, FileZilla,
Remote Desktop, Internet Explorer, Mozilla, Firefox, Thunderbird,
Netscape, Opera, XanaNews ...), and all of them have text fields that do
not insert a tab character on Ctrl-TAB. I found very few text fields that
did exhibit that behavior.
Quote
>Are you aware of any official MS specification that says that the TAB
>key should work that way?

No.
If there was one, then I would expect the Swing team to follow it for the
Windows look-and-feel, but not others. But given that it seems to be an
ad-hoc thing, I am not surprised that it is not.
Quote
All browsers I use (explorer, netscape and firefox) also use tab to
traverse.
That is what JTextField does also. I thought you were saying that
JTextField should respond to Ctrl-TAB by inserting a TAB character.
That behavior is quite rare, as far as I can see.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Swap TAB and Ctrl-TAB for JTextArea

Quote
I will go for the simple:
textArea.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent ke){
if(ke.getKeyCode() == KeyEvent.VK_TAB)
{
ke.consume();
[nextComponent].requestFocus();
}}});

It seems to work pretty good.
I also wanted to implement SHIFT-TAB but couldn't find any
KeyEvent.[SHIFT-TAB] so I went to:

I am sure you have figured it out by now, but I was facing the same
problem today and thought it might be useful for someone:
I added the following code to the constructor of the small wrapper class
(TTextArea) which extends the JTextArea
this.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
if (ke.getKeyCode() == KeyEvent.VK_TAB &&
ke.isShiftDown())
{
ke.consume();
TTextArea.this.transferFocusBackward();
}
else if (ke.getKeyCode() == KeyEvent.VK_TAB)
{
ke.consume();
TTextArea.this.transferFocus();
}
}
});
Tony.