Having a JFormattedTextField allow only int 1 to 9

W

Wayne Mote

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
 
F

Filip Larsen

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,
 
T

Thomas Weidenfeller

Wayne said:
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
 
S

Steve W. Jackson

Thomas Weidenfeller said:
Did you read them? :)))


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 =
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top