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 / GUI / January 2004

Tip: Looking for answers? Try searching our database.

Formatting JTextFields

Thread view: 
Karsten Wutzke - 01 Jan 2004 00:36 GMT
Hi all!

I had this some time ago, but I didn't have the time. What is the best
way to accomplish allowing only certain characters to be entered in a
text field?

I remember having read about something subclassing Document, the text
field model, but it seems a little complicated to me.

I need a textfield where only letters can be entered, possibly a-z and
one field, where only digits are allowed.

Can anyone tell me how to do this, probably providing some short code
snippet?

Thanks for your help!

Karsten
ak - 01 Jan 2004 02:38 GMT
> I had this some time ago, but I didn't have the time. What is the best
> way to accomplish allowing only certain characters to be entered in a
[quoted text clipped - 8 lines]
> Can anyone tell me how to do this, probably providing some short code
> snippet?

something like this:

public class NumericDocument extends PlainDocument {

   public void insertString(int offs, String str, AttributeSet a) throws
BadLocationException {
       StringBuffer sb = new StringBuffer(str.length());

       for(int i = 0; i < str.length(); i++) {
           char c = srt.charAt(i);
           if(Character.isDigit(c)) {
               sb.append(c);
           }
       }
       String nstr = sb.toString();
       if(nstr.length() > 0) {
           super.insertString(nstr);
       }
   }
}

--

____________

http://reader.imagero.com the best java image reader.
Andrew Thompson - 01 Jan 2004 07:48 GMT
...
> I had this some time ago, but I didn't have the time. What is the best
> way to accomplish allowing only certain characters to be entered in a
> text field?

'best' is difficult to define, it depends a lot on the context.

> I remember having read about something subclassing Document, the text
> field model, but it seems a little complicated to me.
>
> I need a textfield where only letters can be entered, possibly a-z and
> one field, where only digits are allowed.

I used a simple method to ensure my Calculator
http://www.physci.org/launcher.jsp#Calculator

Particularly the 'formatNumber' method,
it is invoked by a 'key typed' event, and
attempts to parse the number as a double
(in this case) - it it is not a double, it is
rejected.

You might get some ideas from looking
at that code.

HTH

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
hiwa - 01 Jan 2004 10:51 GMT
Extend javax.swing.InputVerifier and write its/their verify() method(s) accordingly.
<code>
 public boolean verify(thisTextField){
   String str = thisTextField.getText();
   int i = 0;
   while (i < str.length()){
     if (! Character.isDigit(str.charAt(i))){
       showErrorMessage();
       return false;
     }
     ++i;
   }
   return true;
 }
Then,
<code>
 yourJTextField1.setInputVerifier(yourInputVerifier1);
 yourJTextField2.setInputVerifier(yourInputVerifier2);
 //etc.
</code>

> Hi all!
>
[quoted text clipped - 14 lines]
>
> Karsten
ak - 01 Jan 2004 12:36 GMT
> > I had this some time ago, but I didn't have the time. What is the best
> > way to accomplish allowing only certain characters to be entered in a
> > text field?

> Extend javax.swing.InputVerifier and write its/their verify() method(s) accordingly.

however with InputVerifier you can't prevent user from entering illegal
chars.

____________

http://reader.imagero.com the best java image reader.
ak - 03 Jan 2004 14:05 GMT
> Hi all!
>
[quoted text clipped - 14 lines]
>
> Karsten

http://java.sun.com/developer/JDCTechTips/2001/tt1120.html#tip1

____________

http://reader.imagero.com the best java image reader.


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.