Formatted text field

Discussion in 'Java' started by John M, Dec 20, 2003.

  1. John M

    John M Guest

    Hello,

    how is it possible to enable only numbers input in a jFormattedTextField?
    I use now this code:
    JFormattedTextField jFormattedTextField1 = new JFormattedTextField(new
    DecimalFormat());

    But in this case user can enter any character and the decimal conversion
    comes after leaving the cell.
    I want that user can enter only digits.

    Thanks!
     
    John M, Dec 20, 2003
    #1
    1. Advertising

  2. John M

    Sudsy Guest

    John M wrote:
    > Hello,
    >
    > how is it possible to enable only numbers input in a jFormattedTextField?
    > I use now this code:
    > JFormattedTextField jFormattedTextField1 = new JFormattedTextField(new
    > DecimalFormat());
    >
    > But in this case user can enter any character and the decimal conversion
    > comes after leaving the cell.
    > I want that user can enter only digits.
    >
    > Thanks!


    You need to refer to the javadocs first! There's an example of using
    an InputVerifier in the JFormattedTextField page which address
    precisely what you are asking. Save the bandwidth for the thorny
    ones, please.
     
    Sudsy, Dec 20, 2003
    #2
    1. Advertising

  3. "John M" <> wrote in message
    news:hsXEb.151792$...
    > Hello,
    >
    > how is it possible to enable only numbers input in a jFormattedTextField?
    > I use now this code:
    > JFormattedTextField jFormattedTextField1 = new JFormattedTextField(new
    > DecimalFormat());
    >
    > But in this case user can enter any character and the decimal conversion
    > comes after leaving the cell.
    > I want that user can enter only digits.


    Som is '-1' valid for your field, it is an integer,
    but does not consist of only digits..

    If you _only_ need digits, you might try this..

    Add a keylistener to a textfield. Parse the
    contents as an integer on each keypress, if
    that throws an exception get the text of the
    field, trim it by 1, put it back and inform the user..

    [ This has some problems if the user types
    987, then arrows back 9 and types a,
    but I think you get the idea.. ]

    HTH

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Dec 20, 2003
    #3
  4. John M

    ak Guest

    > how is it possible to enable only numbers input in a jFormattedTextField?

    you can have only numbers also in JTextField.
    Use Document which accepts only digits. It is much better then KeyListener.


    ____________

    http://reader.imagero.com the best java image reader.
     
    ak, Dec 21, 2003
    #4
  5. "ak" <> wrote in message news:bs2q78$ve6$...
    > > how is it possible to enable only numbers input in a

    jFormattedTextField?
    >
    > you can have only numbers also in JTextField.
    > Use Document which accepts only digits. It is much better then

    KeyListener.

    This was about integers, right?

    '-3' and '1.73E+3' are both integers,
    ...aren't they (shrugs)?
     
    Andrew Thompson, Dec 21, 2003
    #5
  6. John M

    ak Guest

    > > you can have only numbers also in JTextField.
    > > Use Document which accepts only digits. It is much better then

    > KeyListener.
    >
    > This was about integers, right?
    >
    > '-3' and '1.73E+3' are both integers,
    > ..aren't they (shrugs)?
    >

    ok, not only digits. Use DecimalFormat _in_ Document of JTextField.

    ____________

    http://reader.imagero.com the best java image reader.
     
    ak, Dec 21, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry Manner
    Replies:
    5
    Views:
    570
    Jonathan N. Little
    Jun 9, 2005
  2. JDP@Work

    Help: Sort Date dg col , Add formatted field to a ds?

    JDP@Work, Feb 19, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    121
    JDP@Work
    Feb 19, 2005
  3. Hugh Welford
    Replies:
    3
    Views:
    161
    Hugh Welford
    May 21, 2004
  4. Sound
    Replies:
    2
    Views:
    465
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    451
Loading...

Share This Page