Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

Having a JFormattedTextField allow only int 1 to 9

Thread view: 
Wayne Mote - 23 Jul 2005 10:01 GMT
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
Filip Larsen - 23 Jul 2005 12:49 GMT
> 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,
Signature

Filip Larsen

Thomas Weidenfeller - 25 Jul 2005 10:47 GMT
> 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

Signature

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/

Steve W. Jackson - 27 Jul 2005 16:30 GMT
> > Can any kind person tell me exactly how?
> > I have download the docs and I have two books on Java 2.
[quoted text clipped - 20 lines]
>
> /Thomas

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

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.