• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing the function of the Tab key

 
Brian Drelling
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Quick and easy question... So I have my QuestionBuilder which I have been asking about, and things are going great. However, I'd like to give users the option to use the Tab key to quickly jump from box to box, button to button.

I made a KeyListener class and was working on it, until I realized that using tab to switch focus was already part of JTextFields and JButtons... all this work on a Listener for nothing!

Except JTextArea creates an indent when I press Tab, it does NOT change focus. So is there an easier way to maybe... I dunno... disable indenting and make the Tab key work like normal?

Is there some secret 'disableIndent()' method somewhere or something?
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all you should not be using a KeyListener to even attempt to change this. You should be using Key Bindings. Key Bindings explains why the Tab character is inserted into the text area.

Read the section from the Swing tutorial on "How to Use Key Bindings". While your at it read the section on "How to Use the Focus Subsystem", which gives background information about why the solution below works:

 
Brian Drelling
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I knew I shouldn't use KeyListeners right after I attempted it--sorry if I didn't make that clear.

Also, I read all about the focus subsystem but didn't see a clear answer, so thank you for including the bit about Key Bindings. I must have looked over it or not read what I needed to read--there was a lot of text. :/


Thanks a lot! I appreciate it.
 
santhosh varala kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, swing enabled focus on the components to switch one component to another using tab. so until or unless if each component setFocusable is true. This concept will work fine
 
Brian Drelling
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default? I'm sorry if I am not understanding your post, but the default action is NOT switching off of a Text Area.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!