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 / January 2006

Tip: Looking for answers? Try searching our database.

plaindocument gettext() not working...

Thread view: 
tiewknvc9 - 18 Jan 2006 15:50 GMT
Hi,

I have created a jtextfield that uses a defaultModel of a
PlainDocument.

I do this so that I can limit the input in the textfield to 3 digits.

The problem that seems to have arisen is that when I call
myTextField.getText(), nothing is returned unless specifically set with
setText();

This is especially annoying since I am trying to get user input in the
field.

Does anyone know why this is happening?  What can I do to fix this?

Thanks
teliot@teliot.com - 18 Jan 2006 15:53 GMT
your going to need to paste your code :\
tiewknvc9 - 18 Jan 2006 15:59 GMT
ok.

when I call
myVar.getText(), I get nothing returned unless I use setText() to set
the text of the textfield....

Thanks for the help!

this is the jtextfield class...
----------------------------------------------------

public class TextField3Nums extends JTextField implements
FocusListener{

    public static String m_strVal;

    public TextField3Nums(String str) {
       super(str);
       m_strVal = str;

       this.addFocusListener(this);
   }

    public void focusGained(FocusEvent e) {}; // not needed

    public void focusLost(FocusEvent e) {
       // do checking
        if (this.getText().length() == 0){
            //set default value on exit from field
            this.setText(m_strVal);
        }
    }

   protected Document createDefaultModel() {
         return new PlainDoc3Nums();
   }

   static class PlainDoc3Nums extends PlainDocument {

       public void insertString(int iOffset, String str, AttributeSet
ats)
           throws BadLocationException {

           char[] insertChars = str.toCharArray();

           boolean valid = true;
           boolean fit = true;
           if (insertChars.length + getLength() <= 4) {
               for (int i = 0; i < insertChars.length; i++) {
                   if (insertChars[i] == '.'){
                       valid = true;
                       break;
                   }
                   if (!Character.isDigit(insertChars[i])) {
                       valid = false;
                       break;
                   }
                   //limit to 3 numbers only, . not to be included
                   if (Character.isDigit(insertChars[i])){
                       if (insertChars.length + getLength() == 4){
                           if (!this.getText(0,3).contains(".")){

                               valid = false;
                               break;
                           }
                       }
                   }
               }
           }
           else{
               fit = false;
           }

           if (fit && valid){
               super.insertString(iOffset, str, ats);
           }else if (!fit){
               //do nothing
           }

           if (getLength() == 0){
               super.insertString(iOffset, m_strVal, ats);
           }
       }

       //protected void
removeUpdate(AbstractDocument.DefaultDocumentEvent chng){
           //super.removeUpdate(chng);

           /*if (getLength() == 1){
               try{
                   insertString(1, m_strVal, null);
               }catch(BadLocationException ble){
                  
               }
           }*/
       //}
      
   }
}


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



©2009 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.