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 / April 2004

Tip: Looking for answers? Try searching our database.

Setting the size of a JTextField

Thread view: 
Rui Pacheco - 20 Apr 2004 00:20 GMT
Hi everyone

I'd like to set the size of a JTextField and at the same time limit
the number of characters that can be entered by the user to a higher
number, i.e, the user can see 30 characters but can write 50.

How can I do this?

TIA
Rui Pacheco
ak - 20 Apr 2004 01:37 GMT
> I'd like to set the size of a JTextField and at the same time limit
> the number of characters that can be entered by the user to a higher
> number, i.e, the user can see 30 characters but can write 50.

PlainDocument doc = new PlainDocument() {
   public void insertString(int offset,  String str, AttributeSet a)
                                                     throws
BadLocationException {

       if(offset >= 50) {
           return;
       }
       int max = Math.min(50 - offset, str.length);
       if(max < str.length) {
           str = str.substring(0, max);
       }

       super.insertString(offset, str, a);
   }
};

JTextField tf = new JTextField(doc, "", 30);

--

____________

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.