代码之家  ›  专栏  ›  技术社区  ›  Zoot fatih dogu

在Java代码中忽略CopyLoSoT()、SWT、验证或其他SWT侦听器

  •  0
  • Zoot fatih dogu  · 技术社区  · 15 年前

    在实际的SWT侦听器之外,是否有任何方法可以通过代码忽略侦听器?

    例如,我有一个实现SWT文本窗口小部件的Java程序,并且这些小部件有:

    • 验证监听器过滤掉不需要的文本输入。
    • modifyListeners等待正确数量的有效输入字符,并自动将focus(使用setfocus())设置为下一个有效字段,跳过选项卡顺序中的其他文本小部件。
    • FocusLost(FocusEvent)FocusListeners,等待文本小部件失去焦点以执行额外的输入验证,并根据用户输入执行SQL查询。

    我遇到的问题是清除文本小部件。其中一个小部件的格式为“”(四个数字,一个连字符,然后两个数字),我已经实现了这个监听器,它是SWT代码段的修改版本。 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侦听器解析这个文本小部件中的空格,然后为了避免不必要的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。请验证文本小部件上存在的侦听器。

    有没有建议在代码中有选择地忽略这些侦听器,但在所有其他用户输入中都保留这些侦听器?

    提前感谢您的帮助。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Zoot fatih dogu    14 年前

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

    例子:

     // 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);
    

    只要确保在您完成了您希望以编程方式实现的操作之后重新启用侦听器,否则您的代码将不会像您期望的那样工作。

        2
  •  1
  •   True Soft    15 年前

    我没有测试你的代码,但是尝试方法 isFocusControl() 在侦听器中查看用户是输入文本还是使用 setText() 当焦点不在时 textBox .