This discussion is archived
3 Replies Latest reply: Oct 25, 2011 6:25 AM by 896129 RSS

TextField maximum character restriction

895797 Newbie
Currently Being Moderated
i have a textfield then must include maximum 2 character, i handle it by the following way;

data.setOnKeyPressed(new EventHandler<KeyEvent>(){
public void handle(KeyEvent e){
if(data.getText().length()>1)
{
data.deletePreviousChar();
}
}
}

but, is there any attribute to restrict the textfield's length automaticly?
  • 1. Re: TextField maximum character restriction
    RichardBair Journeyer
    Currently Being Moderated
    No not yet. There was such a feature but it wasn't ready for the GA so I had to remove it. The problem with your implementation is that it doesn't take pasted code into account. I think what I would do is subclass TextField and create a new Content to be used. This new implementation of Content would wrap the normal one, but would impose the two character limit.

    I think that would work, though I haven't tried it.

    BTW, the wrinkle I hadn't resolved (which is why this maximumLength property didn't make it in the 2.0 GA for TextField) was what should happen when a TextField has 20 characters, but then programmatically the maximum length is set to 15 chars. Do we throw away the last 5 chars? Throw an exception? Allow the TextField length to exceed the maximumLength?

    I guess the right answer is to throw away the last 5 chars and enforce the invariant that the number of chars is always <= the maximumLength. But I didn't have time to sort it out. What do you think?

    Richard
  • 2. Re: TextField maximum character restriction
    giovanni_stiwes Newbie
    Currently Being Moderated
    Richard Bair wrote:
    I guess the right answer is to throw away the last 5 chars and enforce the invariant that the number of chars is always <= the maximumLength.
    I agree.
  • 3. Re: TextField maximum character restriction
    896129 Newbie
    Currently Being Moderated
    Richard,

    you stated: "I would do is subclass TextField and create a new Content to be used. This new implementation of Content would wrap the normal one, but would impose the two character limit."

    Could you give us a hint, how you'd like to subclass TextField and wrap content? I tried to do so but I failed because content is declared final.

    Cheers,
    Marc

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points