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

时间:2011-09-23 21:41:15

标签: java text swt eclipse-rcp decimal

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

我找到了两个答案:

  1. 首先,实现VerifyKeyListener和VerifyListener,适用于法语十进制表示法,但简单易行:
  2. 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/
    2. 有人看到另一种解决方案吗?

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

  • 对于Double值的验证,您必须考虑像E这样的字符可能包含在双打中,而像“4e-”这样的部分输入在键入时应该有效,以便最终输入“4e-3”。部分表达式可能会为Double.parseDouble(partialExpression)提供NumberFormatException

  • 另见以下相关问题的答案: How to set a Mask to a SWT Text to only allow Decimals

  • (另请注意:如果只想允许整数值,则可以使用Spinner而不是文本字段。)

答案 2 :(得分:0)

为什么在窗口小部件上没有简单的正则表达式,如下所示:

    if(Text.getText().matches("^[0-9]+$")) {
       show an error dialog or similar
    }