• Post Reply Bookmark Topic Watch Topic
  • New Topic

Numbers only TextField

 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I ensure that only numbers and minus sign (-) is entered in my TextField, I thought of adding a KeyListener and checking for keyChar in the keyPressed method, but then there are far too many special cases (delete key, backspace, copy(CTRL + C) etc) to take care of. Is there any other elegant way of doing this?
- Manish
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,
You need to create a class that extends JTextField and override the createDefaultModel() method to return a Document that limits what can be entered into the field. That basically means that you will need to create another class that extends PlainDocument and you will override the insertString() method there to accomplish your task.
Here is an excerpt from my Developer assignment. It's rather lengthy, but you should get the idea.

Michael Morris
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to extend JTextField. You can just use the class that extends PlainDocument. You use the setDocument method of a textfield.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with 1.4 you can use JFormattedField - see the API, and the Swing Tutorial.
cheers
chantal
 
mo ti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it does not work
it does not get any character
not number and not letters
please help me fix it

thanks
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Tell The Details. "It doesn't work" isn't really helpful.
 
mo ti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't realy know why
everything in that code looks OK
but when I added the Numbers only TextField
and tryed to add numbers to it
it rejected them

here is my executable code:




import java.awt.*;


import javax.swing.*;


public class Exec
{
private JFrame frame;
private JLabel lab;
private SeatsField tf1;




public Exec()
{
frame = new JFrame("Registration Form");
frame.setLayout(new GridLayout(10,1));

lab =new JLabel("only numbers");
tf1 = new SeatsField();

frame.add(lab);
frame.add(tf1);


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void go()
{
frame.setSize(200,300);
frame.setVisible(true);
}



public static void main(String args[])
{
Exec demo = new Exec();
demo.go();
}
}

could you help me?
thanks
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
available of SeatsDocument remains 0. You will need to call setAvailable, and only values between 1 and that value (inclusive) are allowed. Integer.MAX_VALUE is a good choice if you want to allow any integer value.

If you forget to call setAvailable, only values larger > 0 and <= 0 are allowed. Since those two are contradictory no number will match.

Also, please Use Code Tags in the future.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!