Hello everyone. I had a quick question. How can I go about setting constraitnts for a JTextField. I'm trying to not allow users to enter any non numeric numbers, except for dollar signs, commas and periods. Also how can I restrict a JTextField to only allowing two numbers to be entered. Like from 0-99. May someone give me an example to both please. Thanks in advance.

10 Years
Discussion Span
Last Post by NycNessyness

set up a KeyListener to it which refuses to allow anything you don't want.

May I get an example of how I would do that please. I don't know how to set up constraints at all. I don't really need it for what I'm doing but it would save me some trouble from throwing exceptions, unless the user copy and pastes the info into the textfield.


I tried some of the codes out and still couldn't find a solution. I'll give an example of what I'm trying to do. If I have a field named "JTextField num= new JTextField(10);", how can I make this particular field only accept number formats. It won't allow the user to input any letters at all before they even select enter.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.