Java Tips Weblog

  • Blog Stats

    • 1,103,987 hits
  • Categories

  • Archives

Text Field Auto Tab

Posted by Rob Camick on October 25, 2009

Anybody who has ever written a DocumentFilter has probably seen the DocumentSizeFilter example from the Swing tutorial on “Implementing a DocumentFilter”. It is simple, straight forward and works well. However, wouldn’t it be nice to have some additional functionality, namely the ability for auto tabbing when the Document is full?

Actually, I’m a little suprised that auto tabbing is not a property of a JTextField. Implementing auto tabbing functionality in a DocumentFilter is not as staight forward as you might expect. Remember, a Document can be shared by multiple text components, so the main problem is finding the focusable text component that is updating the Document.

The SizeDocumentFilter improves upon the DocumentSizeFilter by adding auto tabbing support. A SizeDocumentFilter can be used in two ways depending on how the size is specified:

  • fixed size – a size is specified when the filter is created. The filter can be added to any number of text components, and the Document size will be the same for all Documents
  • variable size – a size of 0 is specified when the filter is created. This can only be used when adding the filter to JTextFields. In this case the size of the Document is determined by invoking the getColumns() method of the text field. Therefore the filter can be used by multiple text fields with each Document size being different.

The code to create a variable sized filter would be something like:

JTextField tf1 = new JTextField(3);
JTextField tf2 = new JTextField(4);
SizeDocumentFilter sf = new SizeDocumentFilter();
sf.installFilter(tf1, tf2);

You can still add the filter directly to the AbstractDocument if you wish, I just created the installFilter() method as a convenience method.

AbstractDocument doc = (AbstractDocument)tf1.getDocument();
doc.setDocumentFilter( sf );

Auto tabbing is turned on by default but can be changed by using the setAutoTab(…) method. It is anticipated the SizeDocumentFilter would most commonly be used on a JTextField, although there is no reason it can’t be used on a JTextArea or JTextPane.

Now you have another simple filter for your toolkit. The SizeDocumentFilter can also be used with a ChainedDocumentFilter introduced last week.

Get The Code

SizeDocumentFilter.java
ChainedDocumentFilter.java

See Also

Chaining Document Filters

Related Reading

Java Tutorials: Implementing a Document Filter

About these ads

5 Responses to “Text Field Auto Tab”

  1. Ricky said

    I don’t fully understand. What is autoTabbing? Can you shortly explain the feature. Do native textfields have it?

    • Rob Camick said

      Normally in order to changes focus from one text field to another component you use the Tab key. Auto tabbing causes focus to change automatically as you type when the text field is full. This makes it easier to fill out forms. The best way to understand it is to try the code. Try the code as is and then try it with auto tabbing turned off to see the difference.

  2. Ricky said

    I tested the code. Now that I know what I was looking for I know what you meant.

    I guess this only makes sense if the textfield has a limited length which isn’t the case by default.

    Thanks!

  3. PhiLho said

    I stared at the article wondering what is auto-tabbing too, then the words clicked in place! Actually, I recently saw this feature when I installed a software with license key made of several 4-chars fields (making inconvenient to copy and paste the whole key, but that’s another story): once you typed the 4 chars, it automatically goes the next field. So you can type continuously the 16 chars without having to hit movement keys. I haven’t tried if removing all chars of a field makes caret goes to previous field…

    Thanks for sharing (and making some of us to discover the feature exists in Java!).

    • Rob Camick said

      I recently saw this feature when I installed a software with license key made of several 4-chars fields

      Yes, that is one example of when you might use this, although in that case a JFormatted text field might be a better choice since as you say you may want to copy/paste to the entire group.

      This solution is intended for forms where you have multiple unrelated text fields that have size restrictions. The idea is to give you a choice and let you decide which solution is appropriate.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 88 other followers

%d bloggers like this: