Having a JFormattedTextField allow only int 1 to 9

Discussion in 'Java' started by Wayne Mote, Jul 23, 2005.

  1. Wayne Mote

    Wayne Mote Guest

    Hello

    Im new to Java but not programming, I am totally frustrated with getting a
    text field in Java
    to only accept the input of one integer between the values of 1 to 9. I have
    tried so many different
    ways but none seem satisfactory. I imagine it is quite simple when one knows
    how. Im hopeing
    that Java is not as complicated as it seems. It seems unbelievably complex
    to do such a simple thing.

    Can any kind person tell me exactly how?
    I have download the docs and I have two books on Java 2.
    But Im lost.

    Thank you
     
    Wayne Mote, Jul 23, 2005
    #1
    1. Advertising

  2. Wayne Mote

    Filip Larsen Guest

    Wayne Mote wrote

    > Im new to Java but not programming, I am totally frustrated with
    > getting a text field in Java to only accept the input of one integer
    > between the values of 1 to 9.


    Try look at the JFormattedTextField and MaskFormatter, e.g:

    MaskFormatter formatter = new MaskFormatter("#");
    formatter.setValidCharacters("123456789");
    JFormattedTextField tf = new JFormattedTextField(formatter);


    You may also consider using a JComboBox with the numbers 1-9 which
    generally gives better user interfaces when selecting rather than typing
    in is important, for instance:

    Object[] choices = { "Please select", "1", "2", "3", "4", "5", "6",
    "7", "8", "9" };
    JComboBox cb = new JComboBox(choices);

    If you have a sensible default value you should probably set that and
    remove the "Please select" item. Note that you can use pretty much
    whatever objects you like as items (as implied by the Object[] type
    above) as long as the objects have either a sensible toString() value or
    you install a renderer on the combo box.


    Regards,
    --
    Filip Larsen
     
    Filip Larsen, Jul 23, 2005
    #2
    1. Advertising

  3. Wayne Mote wrote:
    > Can any kind person tell me exactly how?
    > I have download the docs and I have two books on Java 2.


    Did you read them? :)))

    > But Im lost.


    Consider working through Sun's GUI tutorial from top to bottom:

    http://java.sun.com/docs/books/tutorial/uiswing/index.html

    Oh, in case no one has told you this before:

    Yes, it takes time and effort to get an overview and some understanding
    about Java, and the Java GUI system. And despite what people tell in
    general, GUI programming was and is not simple (the huge amount of bad,
    ugly, non-functional GUIs out there speak volume).

    If you need instant gratification, then GUI programming is not the thing
    to do. You will also not master GUI programming if you just copy code
    and/or calling methods at random. You need to understand what you are
    doing, and this requires some effort.

    /Thomas

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Jul 25, 2005
    #3
  4. In article <dc2cfl$r8v$>,
    Thomas Weidenfeller <> wrote:

    > Wayne Mote wrote:
    > > Can any kind person tell me exactly how?
    > > I have download the docs and I have two books on Java 2.

    >
    > Did you read them? :)))
    >
    > > But Im lost.

    >
    > Consider working through Sun's GUI tutorial from top to bottom:
    >
    > http://java.sun.com/docs/books/tutorial/uiswing/index.html
    >
    > Oh, in case no one has told you this before:
    >
    > Yes, it takes time and effort to get an overview and some understanding
    > about Java, and the Java GUI system. And despite what people tell in
    > general, GUI programming was and is not simple (the huge amount of bad,
    > ugly, non-functional GUIs out there speak volume).
    >
    > If you need instant gratification, then GUI programming is not the thing
    > to do. You will also not master GUI programming if you just copy code
    > and/or calling methods at random. You need to understand what you are
    > doing, and this requires some effort.
    >
    > /Thomas


    Now *that* is some quality advice. Perhaps this "gentle" reminder
    should be part of your FAQ. :)

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Jul 27, 2005
    #4
    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. Schnoffos
    Replies:
    2
    Views:
    1,236
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,681
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    467
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    980
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    451
    James Kanze
    Apr 17, 2007
Loading...

Share This Page