JTextField limit length Ok, but can't set initial text string through constructor

Discussion in 'Java' started by Lovely Dola, Dec 16, 2003.

  1. Lovely Dola

    Lovely Dola Guest

    I have the following code that limits number of characters
    that can be input to a text field. However, when I pass
    an initial string to the constructor, it is not placed in
    the text field. Can anyone tell me how to do that successfully
    without calling setText() again?

    Please forgive my poor English...


    //*****************************************************************************
    class JTextField2 extends JTextField {
    private int maxLength;

    JTextField2(String text, int columns) {
    super(text, columns);
    maxLength = columns;
    if (maxLength <= 0) maxLength = -1;
    addFocusListener(this);
    }

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

    class PlainDocument2 extends PlainDocument {
    public void insertString(int offset, String str, AttributeSet attr)
    throws BadLocationException {
    if (str == null) return;
    if (maxLength == -1 || getLength() + str.length() <= maxLength) {
    super.insertString(offset, str, attr);
    }
    }
    }
    }

    ....
    // "Hello" not appears in the textfield
    JTextField2 myfld = new JTextField2("Hello", 10);
    myfld.setText("Hello"); // "Hello" is now placed in textfield
     
    Lovely Dola, Dec 16, 2003
    #1
    1. Advertising

  2. Lovely Dola <> wrote:



    > I have the following code that limits number of characters
    > that can be input to a text field. However, when I pass
    > an initial string to the constructor, it is not placed in
    > the text field. Can anyone tell me how to do that successfully
    > without calling setText() again?



    Put the maximum length instance variable into the Document.
    Then do not use createDefaultModel, but the JTextField constructor
    that takes a Document.

    (Then, do not subclass JTextField only for this)


    JTextField2(String text, int columns)
    {
    super(new PlainDocument2(columns), text, columns);
    }


    Christian
     
    Christian Kaufhold, Dec 16, 2003
    #2
    1. Advertising

  3. Lovely Dola

    Lovely Dola Guest

    It works! Thanks a lot!
    But I have a question.
    Why shouldn't subclass JTextField only for this?


    (Christian Kaufhold) wrote in message news:<>...
    > Lovely Dola <> wrote:
    >
    >
    >
    > > I have the following code that limits number of characters
    > > that can be input to a text field. However, when I pass
    > > an initial string to the constructor, it is not placed in
    > > the text field. Can anyone tell me how to do that successfully
    > > without calling setText() again?

    >
    >
    > Put the maximum length instance variable into the Document.
    > Then do not use createDefaultModel, but the JTextField constructor
    > that takes a Document.
    >
    > (Then, do not subclass JTextField only for this)
    >
    >
    > JTextField2(String text, int columns)
    > {
    > super(new PlainDocument2(columns), text, columns);
    > }
    >
    >
    > Christian
     
    Lovely Dola, Dec 17, 2003
    #3
  4. Christian Kaufhold, Dec 18, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    3
    Views:
    14,110
    Karl Seguin
    Feb 17, 2005
  2. =?ISO-8859-1?Q?Peter_Valdemar_M=F8rch?=
    Replies:
    2
    Views:
    861
    =?ISO-8859-1?Q?Peter_Valdemar_M=F8rch?=
    Jul 23, 2003
  3. Replies:
    5
    Views:
    3,027
    Andrew Thompson
    Dec 26, 2005
  4. Replies:
    5
    Views:
    667
    John W. Kennedy
    Jan 11, 2007
  5. Radu
    Replies:
    2
    Views:
    631
Loading...

Share This Page