Prevent User From Entering Characters in a Text Field in SWT
Asked Answered
S

3

7

I want the user to be able only to write numbers and spaces.

How can I do that in SWT?

Sally answered 3/12, 2010 at 13:31 Comment(0)
H
6

Use VerifyListener and reject the chars according to user input.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Test {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        final Text text = new Text(shell, SWT.BORDER);
        text.addVerifyListener(new VerifyListener() {

            @Override
            public void verifyText(VerifyEvent e) {
                // allows cut (CTRL + x)
                if (e.text.isEmpty()) {
                    e.doit = true;
                } else if (e.keyCode == SWT.ARROW_LEFT ||
                            e.keyCode == SWT.ARROW_RIGHT ||
                            e.keyCode == SWT.BS ||
                            e.keyCode == SWT.DEL ||
                            e.keyCode == SWT.CTRL ||
                            e.keyCode == SWT.SHIFT) {
                    e.doit = true;
                } else {
                    boolean allow = false;
                    for (int i = 0; i < e.text.length(); i++) {
                        char c = e.text.charAt(i);
                        allow = Character.isDigit(c) || Character.isWhitespace(c);
                        if ( ! allow ) {
                            break;
                        }
                    }
                    e.doit = allow;
                }

            }
        });
        text.setSize(200, 20);
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
Hospitalize answered 3/12, 2010 at 16:21 Comment(5)
thanks for the solution - you can even beautify this to use SWT.BS for keyCode 8.Imre
This is a horrible solution! It doesn't work for paste. It doesn't work for delete-key. What else doesn't it work for? Hard to tell.Soleure
@Soleure i just update the solution to support ctrl+x, ctrl+v and DEL key.Hospitalize
And if the platform uses different keys for those things...?Soleure
The code snippet works well for Windows environment. I am not sure about Linux & Mac.Hospitalize
S
2

Set a VerifyListener on the text control that checks that inserted text contains only digits.

This works much better than solutions using key codes. Since it works on the input it doesn't block operations like paste and delete.

Example:

text.addVerifyListener(new VerifyListener() {
    @Override
    public void verifyText(VerifyEvent e) {
            for (int i = 0; i < e.text.length(); i++) {
                if (!Character.isDigit(e.text.charAt(i))) {
                    e.doit = false;
                    return;
                }
            }
        }
    });
Soleure answered 10/10, 2016 at 12:17 Comment(0)
A
0

In Swing you have a JFormattedTextField which will do exactly that. But I don't think there is anything similar in SWT. You should be able to make some changes to include a JFormattedTextField if that is really what you need. Another option would be to just validate the user's input after they type.

Acculturate answered 3/12, 2010 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.