扩展 addDocumentListener 的功能
我有一个扩展 javax.swing.JTextField
的文本字段,并且还有像这样的文档侦听器
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
extending MyTextField
应该进行此强制检查,并在检查强制字段以及是否给出了某些值后从数据库获取一些信息。我不想在其中编写相同的文档侦听器代码。是否可以扩展我们在 MyTextField
中添加的文档侦听器,或者我应该使用焦点侦听器?
I've a text field which extends javax.swing.JTextField
and also have document listener like this
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));
}
}
});
}
}
Now I want another text field say MyTextField1
extending MyTextField
which should have this mandatory check and also to get some information from DB after checking mandatory field and if some value is given. I do not want to write the same document listener code in that. Is it possible to extend document listener which we added in MyTextField
OR should I go for focus listener?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JTextField
。大多数时候,不需要扩展 Swing 组件。MyTextField
类中执行的操作看起来与JFormattedTextField
的操作非常相似。您可能需要考虑使用该类。MyTextField
类,然后添加另一个DocumentListener
来执行一些额外的检查。您将保留原始功能,因为超类将添加自己的DocumentListener
JTextField
for what you are doing. Most of the times, there is no need to extend the Swing components.MyTextField
class looks remarkably similar to what aJFormattedTextField
does. You might want to consider using that class insteadMyTextField
class and just add anotherDocumentListener
which does some extra checks. You will keep your original functionality since the super class will add its ownDocumentListener
根据您的描述,我假设您需要对文档更改进行视觉反馈验证,其中确切的验证规则是变量。如果是这样,将其分解为
一些伪代码
From your description, I assume that you want a validate-with-visual-feedback-on-document-change where the exact validation rule is the variable. If so, break it down into
some pseudo code