2012-02-05 85 views
2

的功能,我已经延伸javax.swing.JTextField,也有文档侦听这样延长addDocumentListener

public class MyTetField extends javax.swing.JTextFiled{ 
    public MyTextField(){ 
      super(); 
      // Document listener to check mandatory functionality 
      getDocument().addDocumentListener(new javax.swing.event.DocumentListener() { 
      /** 
      * If the text is changed then this event will be fired. 
      */ 
      public void changedUpdate(javax.swing.event.DocumentEvent e) { 
      } 
      /** 
      * If some value is removed then this event is fired. 
      */ 
      public void removeUpdate(javax.swing.event.DocumentEvent e) { 
      } 
      /** 
      * If some value is auto set, this event will be called 
      * @param e The value change event 
      */ 
      public void insertUpdate(javax.swing.event.DocumentEvent e) { 
       if (getText().trim().equals("")){ 
        setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.RED)); 
       }else{ 
        setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.GRAY)); 
       } 
      } 
     }); 
    } 
} 

现在我想另一个文本字段说MyTextField1延长MyTextField应该有这样的强制检查,并得到一个文本字段检查必填字段后是否有来自DB的某些信息,以及是否给出了某些值。我不想在那写相同的文档侦听器代码。是否有可能扩大我们在MyTextField添加或者我应该去为焦点监听文档侦听?

+0

作为@Robin已经提到:没有扩展JSomething,它们被设计为,将被_used_。不,一个焦点侦听器_永远_(第一近似)是一个解决方案,它是太低的水平。 – kleopatra 2012-02-05 10:42:42

回答

3
  1. 无需为您正在做的事情扩展JTextField。大多数情况下,不需要扩展Swing组件。
  2. 你的听众确实在MyTextField类看起来非常相似什么JFormattedTextField一样。你可能要考虑使用类,而不是
  3. 要真正回答你的问题,你可以扩展你的MyTextField类,并添加另一DocumentListener,做一些额外的检查。你会保留原来的功能,因为超类将增加自己的DocumentListener
+0

为先,挑剔的人全ACK:2)AFAIR,格式化文本字段不改变边界;-) 3)技术上是正确的,但不推荐(通过继承层次堆砌的听众可以有奇怪的干扰效应) – kleopatra 2012-02-05 10:37:14

+0

@kleopatra我想每个Swing程序员在他/她的阿森纳这提高了用户的反馈具有JFormattedTextField上的改进版本 – Robin 2012-02-05 11:42:15

3

从你的描述,我假设你想验证与 - 视觉反馈的文档变化,其确切的验证规则变量。如果是的话,把它分解成

  • 验证界面,检查一个字符串值
  • 它使用验证,以决定是否/如何根据验证结果装点给定Choice上的控制器

一些伪代码

public interface TextPredicate { 
    public boolean isValid(String text); 
} 

public class NotEmptyTextPredicate implements TextPredicate { 
    // imlemented to return true/false for not/empty text 
} 

public class OnChangeValidator { 
    private TextPredicate predicate; 
    private JTextComponent textComponent; 
    private DocumentListener listener; 

    public OnChangeValidator(JTextComponent component, TextPredicate predicate) { 
      // assign and register the listener 
      this.predicate = .... 
      ... 
      this.listener = create... 
      component.getDocument().addDocumentListener(listener); 
    } 

    protected void validate() { 
     decorate(predicate.isValid(textComponent.getText()) 
    } 

    protected void decorate(boolean valid) { 
     if (valid) { 
      // set normal visual properties 
     } else { 
      // set error visual properties 
    } 


    protected DocumentListener createDocumentListener() { 
     DocumentListener l = new DocumentListener() { 
      @Override 
      public void insertUpdate(...) { 
       validate(); 
      } 

      @Override 
      public void removeUpdate(...) { 
       validate(); 
      } 
     }; 
     return l; 
    } 
}