• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Changing the function of the Tab key

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic