在Java中验证整数值时出现问题

在Java中验证整数值时出现问题,java,eclipse-rcp,validation,Java,Eclipse Rcp,Validation,您好,我使用的是EclipseRCP,我需要验证只接受整数值的文本框,因为我已经使用了代码 txtCapacity.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent EVT) { if((EVT.character>='0' && EVT.character<='9')){

您好,我使用的是EclipseRCP,我需要验证只接受整数值的文本框,因为我已经使用了代码

 txtCapacity.addKeyListener(new KeyAdapter() {
              public void keyPressed(KeyEvent EVT) {                     

                    if((EVT.character>='0' && EVT.character<='9')){
                          txtCapacity.setEditable(true);                    
                           txtCapacity.setEditable(true);

                      } else {
                          txtCapacity.setEditable(false);
                             System.out.println("enter only the numeric number");                                  
                      }
              }
      });
txtCapacity.addKeyListener(新的KeyAdapter(){
按下公共无效键(KeyEvent EVT){

如果在使用侦听器时((EVT.character>='0'&&EVT.character),则可以清空文本字段,而不是使其不可编辑。可以执行类似操作,代码段基于您的代码

txtCapacity.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent EVT) {                     
             if(!(EVT.character>='0' && EVT.character<='9')){
                    txtCapabity.setText(""); 
             }
        }
});
txtCapacity.addKeyListener(新的KeyAdapter(){
public void keyReleased(KeyEvent EVT){

如果(!(EVT.character>='0'&&EVT.character,您可以使用此函数验证数字是否:

public static int validateInteger(String number) { int i = -1; try { i = Integer.parseInt(number); } catch (NumberFormatException nfe) {} catch (NullPointerException npe) {} return i; } 公共静态int validateInteger(字符串编号) { int i=-1; 试一试{ i=整数.parseInt(数字); } 捕获(NumberFormatException nfe) {} 捕获(NullPointerException npe) {} 返回i; }
如果函数返回的值小于零,则该值不是有效的正数。

要验证
值是否为十进制,只需使用-

try {
        new BigDecimal(value.toString());
        // value is decimal
} catch (NumberFormatException numberFormatException) {
    // value is not decimal
}

您可能应该在文本框上设置一个。您可以实现一个客户文档来筛选有效的输入,以满足您的要求。每次从字段中添加或删除文本时,文档都会检查整个输入是否有效。

另一种可能是使用Nebular的FormattedTextField小部件,请参阅
优点是您只需提供一个模式,无需编写自己的侦听器。

不要使用
KeyListener
!而是使用
VerifyListener
,因为这将处理粘贴、退格和替换

例如


嗯,这个代码也允许字符。是的,我正在使用swtAnsari,非常感谢,我将投票赞成…你能想到允许十进制吗number@Dinup:如果要允许严格十进制,则需要其他事件来处理。否则,如果整数和十进制都适合,则在对多个句点使用相同的方法。我的意思是只允许一个句点,下次找到句点时,请将其切掉并将值重置回textfield。如果(s.length()==0){txtfield.setBackground(Color.PINK);}不工作,但我仍在工作。很抱歉,您可以尝试使用这个chetan txtfield.setBackground(SWTResourceManager.getColor(warnColor));
text.addVerifyListener(new VerifyListener() {
  @Override
  public void verifyText(VerifyEvent e) {
    final String oldS = text.getText();
    final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);

    try {
      new BigDecimal(newS);
      // value is decimal
    } catch (final NumberFormatException numberFormatException) {
      // value is not decimal
      e.doit = false;
    }
  }
});
txtfield.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent evt) {                     

        char c=evt.getKeyChar();
        if(Character.isLetter(c))
        {
            JOptionPane.showMessageDialog(null, "PLEASE ENTER A DIGIT", "INVALID NUMBER", JOptionPane.ERROR_MESSAGE);
            txtfield.setBackground(Color.PINK);
            txtfield.setText("");

            String s = txtfield.getText();
            if(s.length() == 0){
                txtfield.setBackground(Color.PINK);
            }
        }
        else
        {
            txtfield.setBackground(Color.white);
        }            
    }
});