1
DishFormPanelistIdLbl.setText("Panelist ID:");

        DishFormPanelistIdTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DishFormPanelistIdTxtActionPerformed(evt);
            }
        });

I have here a text label and text field for ID i want to input only numbers in text how is it possible?been trying the sample program un net beans but they dont have such a solution for this problem any help is appreciated

"Update"

What i want here is when i type letter nothing will show but when number then it will show

2
  • i did try the sample programs in net beans they dont have such limiting of input all they has is inputting alphanumeric i want numeric only...the way im doing the learning here is doing the sample first hand but it does not have the solution for this Commented Nov 15, 2014 at 8:58
  • If one of these answers helped you out, please mark one as "accepted answer". If they didn't help you, please tell us the problems with the answers given so we can help you further Commented Nov 17, 2014 at 22:51

2 Answers 2

6

What i did is in the design mode i right click the text field > Events > Key > KeyTyped

then in the code i hade something like this

private void DishFormPanelistIdTxtKeyTyped(java.awt.event.KeyEvent evt) {                                               
        // TODO add your handling code here:
        char enter = evt.getKeyChar();
        if(!(Character.isDigit(enter))){
            evt.consume();
        }
    } 

i didnt get the solution here in stack but i will link it this is the link

Sign up to request clarification or add additional context in comments.

1 Comment

minus for?explain because it work if it is not a good please explain because this is what i use now
3

You could do this by using a JFormattedTextField. It's pretty straight forward: you pass it a formatter (which extends AbstractFormatter), and the formatter will allow you to restrict and modify how things are displayed in your field. In this case, you could use the pre-made formatter NumberFormatter:

NumberFormatter formatter = new NumberFormatter(); //create the formatter
formatter.setAllowsInvalid(false); //must specify that invalid chars are not allowed

JFormattedTextField field = new JFormattedTextField(formatter); //pass the formatter to the field

This will also add a comma where ever needed (for example, when you type in 1000, it'll display it as 1,000.

2 Comments

how can i create the formatter should i do public void NumberFormatter() for that?
@HogRider Create the formatter as you see it in my post: 1 - Create a variable. 2 - Store the new NumberFormatter() in it. 3 - Using the variable, call setAllowsInvalid and pass false to it, so it prevents invalid characters (non-numeric characters in this case). 4 - Pass the formatter to the formattedtextfield

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.