Java: Limit maximum number of characters in SWT text field

To limit the number of characters for a SWT text field, add a verify listener to the text field:

textField.addVerifyListener(new VerifyListener() {
    public void verifyText(VerifyEvent e) {
    String oldString = textField.getText();
    String newString = oldString.substring(0, e.start) + e.text + oldString.substring(e.end);

    if(newString.length() > 10) {
        e.doit = false; 
    }
}

Werbung



comments powered by Disqus

Copyright Dunkel & Iwer GbR | Datenschutzerklärung | Impressum