about JTextField

hi,how can i restrict the user to type only 10 charcter in JTextField,if user type's 11th charcater i have to stop typing of the user, means if user type's 11th character nothing will accept on the JTextField.please help about this!.
[263 byte] By [kiran@grdemettlea] at [2007-11-26 21:11:56]
# 1
Add a document listener:textField.getDocument().addDocumentListener(listener);In your listener's insertUpdate() method, if the size of the document exceeds 10 characters, truncate it.Geoff
glevnera at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 2
or use a JFormattedTextField instead: http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html
alienchilda at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 3
how can i truncate the length can u give me any idea please ,i have written the document listener but i dont know how to truncate the length of the JTextFiled.
kiran@grdemettlea at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 4

> how can i truncate the length can u give me any idea

> please ,

> i have written the document listener but i dont know

> how to truncate the length of the JTextFiled.

Get the contents with JTextField.getText(). If the string is too long, change it with JTextField.setText()...

glevnera at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 5
thank you very much , i have solved my problem with your Suggestion.
kiran@grdemettlea at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 6
A better approach would be to use a document filter.Read here: http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#filterYou even have an example of limiting the text length in the tutorial.
Rodney_McKaya at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 7
how can i use single DocumentFilterfor multiple fields ,and each field have different character limit .
kiran@grdemettlea at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 8
DocumentSizeFilter gets the character limit size in the constructor.So register a new DocumentSizeFilter for each field.
Rodney_McKaya at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 9

You could also extend the JTextfield like so:

import java.awt.event.KeyEvent;

import javax.swing.JTextField;

public class RestrictedLengthTextField extends JTextField

{

// constructors here

public void processKeyEvent(KeyEvent evt) {

if(this.getText().length()>=10)

{

evt.consume();

}

super.processKeyEvent(evt);

}

}

Message was edited by:

ignignokt84

ignignokt84a at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 10
> You could also extend the JTextfield like so:Except that your approach won't work for pasted text.Stick with the recommend approach of using a DocumentFilter.
camickra at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 11
can i use MaskFormatter() instead of DocumentFilter,
kiran@grdemettlea at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...
# 12
Yes.
JayDSa at 2007-7-10 2:49:21 > top of Java-index,Desktop,Core GUI APIs...