How to limit the lenght of inputted values in a textfield

How to limit the lenght of inputted values in a textfield

Page 1 of 1

1 Replies - 1365 Views - Last Post: 18 April 2010 - 06:19 PM Rate Topic: -----

#1 stoplookandlisten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-January 10

How to limit the lenght of inputted values in a textfield

Posted 17 April 2010 - 11:58 PM

hi, I appreciate if anyone can help me.

So far this what i got.
I want to limit the length of inputted values into a textfield up to 7 digits.

With this code, the inputted values goes on and on. it is not limiting it to only 7. Only what is displayed, thAt it will show 7 digits. When sum button is click. it adds up all the digits inputted.

What I want is that, I can input up tp 7 digits only.

THANKS

private void jButtonSubtractActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:

         double A;
         double B;
         int MAX_LEN = 7;

          try {
            A = Double.parseDouble(
                this.jTextField1.getText());

            if(jTextField1.getText().length() > MAX_LEN);
            {
            jTextField1.setText(jTextField1.getText().substring(0, MAX_LEN ));
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to limit the lenght of inputted values in a textfield

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: How to limit the lenght of inputted values in a textfield

Posted 18 April 2010 - 06:19 PM

you can use a JFormattedTextField in conjunction with a MaskFormatter
JFormattedTextField:
http://java.sun.com/...dTextField.html
MaskFormatter:
http://java.sun.com/...kFormatter.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1