• Post Reply Bookmark Topic Watch Topic
  • New Topic

No tabbing in JTextPane

 
Chaz Branham
Ranch Hand
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day All,

This may be something simple but I cannot seem to figure out how to make the tab key stop tabbing (ie. putting spaces) inside of a JTextPane. I do have it tabbing to the other componenets by setting up a FocusPolicy class and FocusPolicy Keys but it still adds the space in the JTextPane even though it tabs out.

Any help on this is greatly apprecaited.
 
Chaz Branham
Ranch Hand
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disregard the question. It was simple I just needed to consume the event with my key listener so it stops inserting the tab into the JTextPane.

Thanks.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just needed to consume the event with my key listener


No, you should NOT be using a KeyListener. Swing uses Key Bindings to handle Actions when a given KeyStoke is pressed. So in general you would play with the Key Bindings. Read the section from the Swing tutorial on Key Bindings for more information.

However, in this case you don't event need to worry about key bindings or write your own code to manage the focus because Swing also has its own focus subsystem. The above tutorial also has a section on the focus subsystem you should read.

So the simple solution to your problem is to just restore the default focus keys for your text pane. This is done by using:

 
Chaz Branham
Ranch Hand
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help and recommendation.
 
Chaz Branham
Ranch Hand
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I came up with after reading your recommendation:



This works like a champ but is it the right way? I still needed to use the KeyboardFocusManager and call the focusNextComponent method.

Thanks
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default JTextPane remove Tab from the list of focus keys to the Tab key is passed to the text pane and the default Action for the Tab key inserts a tab character into the text pane.

The (2 line) solution I gave you simply restores Tab to the list of focus keys which means the focus subsystem will now handle the Tab key and the text pane will never receive the event, so you don't need to provide a custom Tab Action. At least that's the way it worked for me when I tested it.

I don't think your custom FocusTraversalPolicy should change any of this behaviour but I don't know. Try my suggestion using the default traversal policy to see if it works the way I explained. Then try adding your custom policy to see if the behaviour changes.
 
Chaz Branham
Ranch Hand
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did try your two line solution and when I used that it never went to the next component and just inserted a tab into the JTextPane so then I wrote the code mentioned earlier to get it to work. I'm not sure what the differences are but the two line solution did not resolve the issue.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what the differences are


I gave one suggestion. I suggest the custom focus traversal policy.

but the two line solution did not resolve the issue.


It works fine. When we give suggestions they are based on the default classes of the JDK. If you write custom code we can't make suggestions because we don't know what your custom code is like.

So the first thing you do is create a SSCCE to test the suggestion and understand the concept. In this case its 6 lines of code. It was tested on JDK7 on Windows 7:



Then you make changes to the code (one at a time) to find out when it stops working.
 
Chaz Branham
Ranch Hand
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great explination and thank you for taking the time in guideing me in the right direction when testing and trying to find the problem areas. I will in the future plan to do this with any recommendations I recieve.

Have a good one
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!