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.

Recommended Answers

All 8 Replies

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

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.

Those things are covered by the tutorials that I linked above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.