0

在实际的 SWT 侦听器之外,有没有办法通过代码忽略侦听器?

例如,我有一个实现 SWT 文本小部件的 java 程序,小部件有:

  • SWT.Verify 侦听器过滤掉不需要的文本输入。
  • ModifyListeners 等待正确数量的有效输入字符并自动将焦点(使用 setFocus())设置到下一个有效字段,跳过 Tab 键顺序中的其他文本小部件。
  • focusLost(FocusEvent) 等待文本小部件失去焦点以执行附加输入验证并根据用户输入执行 SQL 查询的 FocusListeners。

我遇到的问题是清除文本小部件。其中一个小部件的格式为“####-##”(四个数字,一个连字符,然后是两个数字),我已经实现了这个监听器,它是 SWT Snippet Snippet179的修改版本。此文本小部件的初始文本是“-”,以向用户提供有关预期格式的视觉反馈。只有数字是可接受的输入,程序会在适当的位置自动跳过连字符。

     /*
      * This listener was adapted from the "verify input in a template (YYYY/MM/DD)" SWT Code
      * Snippet (also known as Snippet179), from the Snippets page of the SWT Project.
      * SWT Code Snippets can be found at:
      * http://www.eclipse.org/swt/snippets/
      */
     textBox.addListener(SWT.Verify, new Listener() 
  {
   boolean ignore;
   public void handleEvent(Event e) 
   {
    if (ignore) return;
    e.doit = false;
    StringBuffer buffer = new StringBuffer(e.text);
    char[] chars = new char[buffer.length()];
    buffer.getChars(0, chars.length, chars, 0);
    if (e.character == '\b') 
    {
     for (int i = e.start; i < e.end; i++) 
     {
      switch (i) 
      {
       case 0: /* [x]xxx-xx */
       case 1: /* x[x]xx-xx */
       case 2: /* xx[x]x-xx */
       case 3: /* xxx[x]-xx */ 
       case 5: /* xxxx-[x]x */
       case 6: /* xxxx-x[x] */
       {
        buffer.append(' '); 
        break;
       }
       case 4: /* xxxx[-]xx */
       {
        buffer.append('-');
        break;
       }
       default:
        return;
      }
     }
     textBox.setSelection(e.start, e.start + buffer.length());
     ignore = true;
     textBox.insert(buffer.toString());
     ignore = false;
     textBox.setSelection(e.start, e.start);
     return;
    }

    int start = e.start;
    if (start > 6) return;
    int index = 0;
    for (int i = 0; i < chars.length; i++) 
    {
     if (start + index == 4) 
     {
      if (chars[i] == '-') 
      {
       index++;
       continue;
      }
      buffer.insert(index++, '-');
     }
     if (chars[i] < '0' || '9' < chars[i]) return;
     index++;
    }
    String newText = buffer.toString();
    int length = newText.length();
    textBox.setSelection(e.start, e.start + length);
    ignore = true;
    textBox.insert(newText);
    ignore = false;

    /*
     * After a valid key press, verifying if the input is completed 
     * and passing the cursor to the next text box.
     */
    if (7 == textBox.getCaretPosition())
    {
                /*
                 * Attempting to change the text after receiving a known valid input that has no results (0000-00).
                 */
                if ("0000-00".equals(textBox.getText()))
                {
                    // "0000-00" is the special "Erase Me" code for these text boxes.
                    ignore = true;
                    textBox.setText("    -  ");
                    ignore = false;    
                }

     // Changing focus to a different textBox by using "setFocus()" method.
     differentTextBox.setFocus();
    }
   }
  }
  );

如您所见,我想出的从代码中的不同点清除此文本小部件的唯一方法是分配“0000-00”

textBox.setText("000000")

并在侦听器中检查该输入。当接收到该输入时,侦听器将文本改回“ - ”(四个空格,一个连字符,然后是两个空格)。

还有一个 focusLost Listener 会解析这个文本小部件的空格,然后为了避免不必要的 SQL 查询,如果输入无效(即包含空格),它会清除/重置所有字段。

 // Adding focus listener to textBox to wait for loss of focus to perform SQL statement.
 textBox.addFocusListener(new FocusAdapter() 
 {
  @Override
  public void focusLost(FocusEvent evt) 
  {
   // Get the contents of otherTextBox and textBox. (otherTextBox must be <= textBox)
   String boxFour = otherTextBox.getText();
   String boxFive = textBox.getText();

   // If either text box has spaces in it, don't perform the search.
   if (boxFour.contains(" ") || boxFive.contains(" "))
   {
       // Don't perform SQL statements.  Debug statement.
       System.out.println("Tray Position input contains spaces.  Ignoring.");

                //Make all previous results invisible, if any.
                labels.setVisible(false);
                differentTextBox.setText("");
                labelResults.setVisible(false);
   }
   else
   {
             //... Perform SQL statement ...
            }
  }
 }
 );

好的。通常,我在此代码中使用 SWT MessageBox 小部件与用户交流,或者希望在验证输入后将文本小部件更改回空状态。问题是消息框似乎创建了一个 focusLost 事件,并且使用 .setText(string) 方法受 SWT.Verify 文本小部件上存在的侦听器的影响。

关于在代码中选择性地忽略这些侦听器,但让它们出现在所有其他用户输入中的任何建议?

预先感谢您的协助。

4

2 回答 2

2

如果您命名侦听器而不是使用匿名侦听器,则可以随时添加和删除它。

例子:

 // Adding focus listener to textBox to wait for loss of focus to perform SQL statement.
 FocusAdapter focusTextBox = new FocusAdapter() 
 {
  @Override
  public void focusLost(FocusEvent evt) 
  {
   // Get the contents of otherTextBox and textBox. (otherTextBox must be <= textBox)
   String boxFour = otherTextBox.getText();
   String boxFive = textBox.getText();

   // If either text box has spaces in it, don't perform the search.
   if (boxFour.contains(" ") || boxFive.contains(" "))
   {
       // Don't perform SQL statements.  Debug statement.
       System.out.println("Tray Position input contains spaces.  Ignoring.");

                //Make all previous results invisible, if any.
                labels.setVisible(false);
                differentTextBox.setText("");
                labelResults.setVisible(false);
   }
   else
   {
             //... Perform SQL statement ...
            }
  }
 };

然后,您可以通过执行以下操作添加侦听器:

textBox.addFocusListener(focusTextBox);

删除就这么简单:

textBox.removeFocusListener(focusTextBox);

只需确保在完成您想要以编程方式实现的目标后重新启用侦听器,否则您的代码将不会像您期望的那样运行。

于 2011-01-07T20:51:24.157 回答
1

我没有测试您的代码,但是尝试isFocusControl()在侦听器中使用方法来查看用户是否输入了文本,或者您setText()在焦点不在时设置了文本textBox

于 2010-09-15T20:58:14.170 回答