Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextArea size changes on pressing TAB key

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i enter data inside JTextArea, and press TAB key to move to next textfield/ or anything, it doesn't moves. Instead it changes the size of JTextArea each time i press TAB key.
By using "setTabSize(0);" i can stop it from re-sizing the textarea, but how to move to the next textfield?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, a JTextArea accepts tabs as characters for its own content. You need to change the key binding (hint: search for those two words) of the tab button.
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:When i enter data inside JTextArea, and press TAB key to move to next textfield/ or anything, it doesn't moves. Instead it changes the size of JTextArea each time i press TAB key.
By using "setTabSize(0);" i can stop it from re-sizing the textarea, but how to move to the next textfield?


I don't know why a JTextArea would resize if you pressed the TAB key.

However, maybe I'm wrong on this, but I'm wondering if you're needing to set the focus to the next textfield.

For the key binding, I think you use the setKeymap() method for that. Or addKeymap().

You might be able to add a KeyListener and simply tell it what you want to do in the method keyTyped().

http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html

I want to say that you might want to use requestFocusInWindow() or something.

 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why fight the default behavior, which is to press <Ctrl>+<Tab> to advance to the next component in the focus cycle? However, if you have to do this the way forward is to set the focus traversal keys for the text area. Look up the method for that, inherited from JComponent.

A JTextArea is usually displayed inside a JScrolPane. Doing that should take care of any resizing issues. Also, what are the wrapping settings on your text area? If you don't understand that question, read the API for methods that contain "wrap"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic