如何验证swt文本窗口小部件是否为十进制值

问题描述 投票:2回答:3

在swt中,文本小部件允许任何字符串。但是,输入十进制值的最合适的SWT窗口小部件是什么?

我找到了两个答案:

  1. 首先,实现VerifyKeyListener和VerifyListener,适用于法语十进制表示法,但简单易行:
package test.actions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Text;

public final class AmountVerifyKeyListener implements VerifyListener, VerifyKeyListener             {

    private static final String REGEX = "^[-+]?[0-9]*[,]?[0-9]{0,2}+$"; 

    private static final Pattern pattern = Pattern.compile(REGEX);  

    public void verifyText(VerifyEvent verifyevent) {
        verify(verifyevent);
    }

    public void verifyKey(VerifyEvent verifyevent) {
        verify(verifyevent);
    }

    private void verify (VerifyEvent e) {
        String string = e.text;
        char[] chars = new char[string.length()];
        string.getChars(0, chars.length, chars, 0);

        Text text = (Text)e.getSource();

        if ( ( ",".equals(string) || ".".equals(string) ) && text.getText().indexOf(',') >= 0 ) {
            e.doit = false;
            return;
        } 

        for (int i = 0; i < chars.length; i++) {
            if (!(('0' <= chars[i] && chars[i] <= '9') || chars[i] == '.' ||  chars[i] == ',' || chars[i] == '-')) {
                e.doit = false;
                return;
            } 


            if ( chars[i] == '.' ) {
                chars[i] = ',';
            }
        }

        e.text = new String(chars);

        final String oldS = text.getText();
        String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
        Matcher matcher = pattern.matcher(newS);
        if ( !matcher.matches() ) {
            e.doit = false;
            return;
        }

    }
}

和verifyKeyListener关联的主类:

package test.actions;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class TestMain {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(2, false));

        final Text text = new Text(shell, SWT.NONE);

        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        text.addVerifyListener(new AmountVerifyKeyListener() ) ;

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }

}
  1. 使用星云项目中的FormattedText:http://eclipse.org/nebula/

有人看到另一个解决方案?

java text swt eclipse-rcp decimal
3个回答
3
投票

更简洁的方法是使用Double.parseString()并捕获NumberFormatException以确定其中的文本是否可以转换为double。


0
投票
  • 对于Double值的验证,你必须考虑像E这样的字符可能包含在双打中,而像“4e-”这样的部分输入在输入时应该是有效的,以便最终输入“4e-3”。部分表达式可能会为Double.parseDouble(partialExpression)提供NumberFormatException。
  • 另请参阅以下相关问题的答案:How to set a Mask to a SWT Text to only allow Decimals
  • (进一步说明:如果只想允许整数值,可以使用Spinner而不是文本字段。)

0
投票

为什么不在小部件上使用如下的简单正则表达式:

    if(Text.getText().matches("^[0-9]+$")) {
       show an error dialog or similar
    }
© www.soinside.com 2019 - 2024. All rights reserved.