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

[301 byte] By [First_knighta] at [2007-11-27 8:10:44]
# 1

> 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 );

}

}

);

bryanoa at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 2
Did you read the JTextField api? There's an example of a text field that only accepts uppercase characters right in it.
hunter9000a at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 3
i made it like you said but it gave me 100 errors !!!!!!!!!!!!!this is the code
First_knighta at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 4

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);

}

});

First_knighta at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 5
> 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/
hunter9000a at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 6

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 > top of Java-index,Java Essentials,New To Java...
# 7
but i don't see that my code contain syntax error?
First_knighta at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 8

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 );

}

bryanoa at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 9
thanks so much puceyour help is so appreciated
First_knighta at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 10
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.
hunter9000a at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 11
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?
First_knighta at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 12
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.
bryanoa at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 13

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.*;

First_knighta at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 14
oh sorry i've to import import javax.swing.text.*;
First_knighta at 2007-7-12 19:54:28 > top of Java-index,Java Essentials,New To Java...
# 15
import javax.swing.text;
bryanoa at 2007-7-21 22:31:11 > top of Java-index,Java Essentials,New To Java...
# 16

> 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

hunter9000a at 2007-7-21 22:31:11 > top of Java-index,Java Essentials,New To Java...