how to prevent the user from entering a character in JTextfield
hello there i've made 6 JTextField By Looping
JTextField[ ] text =new JTextField[5];
and i want to prevent the user from entering a string(character)
in any of the six text fields,Just only enter numbers?
how to do so?
thank you
> hello there i've made 6 JTextField By Looping
> JTextField[ ] text = new
> JTextField[5];
>
> and i want to prevent the user from entering a
> string(character)
> in any of the six text fields,Just only enter
> numbers?
> how to do so?
> thank you
User a subclass of the Document class:
text.setDocument(
new PlainDocument() {
public void insertString( int offset, String string, AttributeSet attributeSet ) throws BadLocationException {
//remove any non numerics and pass the result to the super.insertString method
super.insertString( offset, string.toUpperCase(), attributeSet );
}
}
);
Did you read the JTextField api? There's an example of a text field that only accepts uppercase characters right in it.
i made it like you said but it gave me 100 errors !!!!!!!!!!!!!this is the code
JTextField[ ] text= new JTextField[5];
text.setDocument(new PlainDocument(){
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
if("0123456789".indexOf(str) < 0)
{
java.awt.Toolkit.getDefaultToolkit().beep();
return;
}
super.insertString(offs, str, a);
}
});
> i made it like you said but it gave me 100 errors> !!!!!!!!!!!!!> this is the codeYou've got a basic syntax error. Here's a tutorial that covers all teh basics. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/
Hi,
I've implemented number fields based on JFormattedTextField.
They also support a min and a max value.
Maybe you find them useful (the library is open source):
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JRealNumberField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JDoubleField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JFloatField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedRealNumberField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedDoubleField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedFloatField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JWholeNumberField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JByteField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JIntegerField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLongField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JShortField.html
Tutorial:
http://softsmithy.sourceforge.net/lib/docs/tutorial/swing/number/index.html
Homepage:
http://www.softsmithy.org
Download:
http://sourceforge.net/project/showfiles.php?group_id=64833
Source:
http://sourceforge.net/svn/?group_id=64833
http://softsmithy.svn.sourceforge.net/viewvc/softsmithy/trunk/lib/src/org/softsmithy/lib/
-Puce
Pucea at 2007-7-12 19:54:28 >

but i don't see that my code contain syntax error?
Try this:
public void insertString( int offset, String string, AttributeSet attributeSet ) throws BadLocationException {
int character = string.charAt( string.length() - 1 );
if ( (character < 48 || character > 57 ) )
string = string.substring( 0, ( string.length() - 1 ) );
super.insertString( offset, string, attributeSet );
}
thanks so much puceyour help is so appreciated
You're calling the setDocument() method on text, which is an array, not a JTextField. You need to do that to each individual element of the array, not to the whole thing. In the loop where you create the JTextField objects, set their document there.
ok,now i want to think in another wayi want to let the user enter whatever numbers or characters in the text field but if he enters a character a messagedialog appear?how to do that check?
Use the same method I have you, just display a dialog if there is a non numeric character entered....You can call another method to display the dialog if you want.
i did as hunter9000 said at last,but i've alittle problem here:
for(int i = 0; i < text.length; i++) text[i] = new JTextField(5);
for(int i = 0; i < text.length; i++){
text[i].setDocument(new PlainDocument(){
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
if("0123456789".indexOf(str) < 0)
{
java.awt.Toolkit.getDefaultToolkit().beep();
return;
}
super.insertString(offs, str, a);
}
});
}
annot find simple class PlainDocument
although a wrote it correctly and i imported
import javax.swing.*;
import java.awt.*;
import java.text.*;
oh sorry i've to import import javax.swing.text.*;
> i did as hunter9000 said at last,but i've alittle
> problem here:
>
> for(int i = 0; i < text.length; i++)
> text[i] = new JTextField(5);
>for(int i = 0; i < text.length; i++){
>text[i].setDocument(new PlainDocument(){
> public void insertString(int offs, String str,
> AttributeSet a) throws BadLocationException{
>if("0123456789".indexOf(str) < 0)
> {
>
> ava.awt.Toolkit.getDefaultToolkit().beep();
>return;
>
> super.insertString(offs, str, a);
>
>});
>
>}
> ot find simple class PlainDocument
> although a wrote it correctly and i imported
> import javax.swing.*;
> import java.awt.*;
> import java.text.*;
Almost. PlainDocument's under javax.swing.text.
From the api:
java.lang.Object
extended by javax.swing.text.AbstractDocument
extended by javax.swing.text.PlainDocument
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/PlainDocument.html