在eclipse-rcp / swt中创建多个相同的文本验证侦听器

时间:2023-01-20 16:02:58

I'm trying to validate the input of multiple text boxes (i.e. they should be a number), and found the useful code snippet below here.

我正在尝试验证多个文本框的输入(即它们应该是一个数字),并在此处找到了有用的代码片段。

However, if I have three text boxes (text, moreText and evenMoreText), how can I apply a verify listener with the same functionality to each, without having to repeat the (.addVerifyListener(new VerifyListener() {...) code three times?

但是,如果我有三个文本框(text,moreText和evenMoreText),我如何应用具有相同功能的验证侦听器,而不必重复(.addVerifyListener(新的VerifyListener(){...)代码三次?

I don't want to implement a switch statement or similar (to decide which text box to apply it to), I want something more generic (that I can perhaps make available for other classes to use in the future).

我不想实现switch语句或类似的(决定将它应用于哪个文本框),我想要更通用的东西(我可以将其用于将来使用的其他类)。

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 {
      BigDecimal bd = new BigDecimal(newS);
      // value is decimal
      // Test value range
    } catch (final NumberFormatException numberFormatException) {
      // value is not decimal
      e.doit = false;
    }
  }
});

1 个解决方案

#1


5  

Define the VerifyListener beforehand and get the actual Text from the VerifyEvent:

事先定义VerifyListener并从VerifyEvent获取实际的Text:

VerifyListener listener = new VerifyListener()
{
    @Override
    public void verifyText(VerifyEvent e)
    {
        // Get the source widget
        Text source = (Text) e.getSource();

        // Get the text
        final String oldS = source.getText();
        final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);

        try
        {
            BigDecimal bd = new BigDecimal(newS);
            // value is decimal
            // Test value range
        }
        catch (final NumberFormatException numberFormatException)
        {
            // value is not decimal
            e.doit = false;
        }
    }
};

// Add listener to both texts
text.addVerifyListener(listener);
anotherText.addVerifyListener(listener);

If you want to use it in other places as well, create a new class:

如果您想在其他地方使用它,请创建一个新类:

public class MyVerifyListener implements VerifyListener
{
    // The above code in here
}

and then use:

然后使用:

MyVerifyListener listener = new MyVerifyListener();

text.addVerifyListener(listener);
anotherText.addVerifyListener(listener);

#1


5  

Define the VerifyListener beforehand and get the actual Text from the VerifyEvent:

事先定义VerifyListener并从VerifyEvent获取实际的Text:

VerifyListener listener = new VerifyListener()
{
    @Override
    public void verifyText(VerifyEvent e)
    {
        // Get the source widget
        Text source = (Text) e.getSource();

        // Get the text
        final String oldS = source.getText();
        final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);

        try
        {
            BigDecimal bd = new BigDecimal(newS);
            // value is decimal
            // Test value range
        }
        catch (final NumberFormatException numberFormatException)
        {
            // value is not decimal
            e.doit = false;
        }
    }
};

// Add listener to both texts
text.addVerifyListener(listener);
anotherText.addVerifyListener(listener);

If you want to use it in other places as well, create a new class:

如果您想在其他地方使用它,请创建一个新类:

public class MyVerifyListener implements VerifyListener
{
    // The above code in here
}

and then use:

然后使用:

MyVerifyListener listener = new MyVerifyListener();

text.addVerifyListener(listener);
anotherText.addVerifyListener(listener);