我正在尝试在许多JTextField上使用DocumentListener。我需要知道哪个textField进入DocumentEvent,所以我可以做一些特定的过程。下面是我如何设置我的DocumentListener
和其中一个JTextField [感谢这个例子:autocompleteFilter](我的textField在更高的范围内声明)。
我该如何解决这个问题?
final DocumentListener documentListener = new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void insertUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
private void printIt(DocumentEvent documentEvent) {
final DocumentEvent.EventType type = documentEvent.getType();
String typeString = null;
final JTextField textField = (JTextField) documentEvent.getDocument().getProperty("parent");
if (type.equals(DocumentEvent.EventType.CHANGE)) {
typeString = "(parent: " + textField + ") Change";
} else if (type.equals(DocumentEvent.EventType.INSERT)) {
typeString = "(parent: " + textField + ") Insert";
} else if (type.equals(DocumentEvent.EventType.REMOVE)) {
typeString = "(parent: " + textField + ") Remove";
}
System.out.print("Type : " + typeString);
final Document source = documentEvent.getDocument();
final int length = source.getLength();
System.out.println("Length: " + length);
}
};
我的JTextField编码如下......
textFieldFencing_LC1 = new JTextField();
textFieldFencing_LC1.setHorizontalAlignment(SwingConstants.CENTER);
textFieldFencing_LC1.setFont(new Font("Tahoma", Font.PLAIN, 9));
textFieldFencing_LC1.setColumns(10);
textFieldFencing_LC1.setBounds(234, 535, 85, 14);
panelLC.add(textFieldFencing_LC1);
textFieldFencing_LC1.getDocument().addDocumentListener(documentListener);
textFieldFencing_LC1.getDocument().putProperty("parent",textFieldFencing_LC1);
我想要的输出应该是这样的
Type : (parent: textFieldFencing_LC1) InsertLength: 1
Type : (parent: textFieldFencing_LC1) InsertLength: 1
我得到的输出看起来像这样......
Type : (parent: javax.swing.JTextField[,234,535,85x14,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@384cdfdd,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=10,columnWidth=0,command=,horizontalAlignment=CENTER]) InsertLength: 2
Type : (parent: javax.swing.JTextField[,234,535,85x14,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@384cdfdd,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=10,columnWidth=0,command=,horizontalAlignment=CENTER]) InsertLength: 3
答案 0 :(得分:0)
在阅读本课程的文档后,我意识到putProperty(Object,Object)
方法允许我将String
放入其中。所以,现在我的JTextField上的监听器看起来像这样......
textFieldFencing_LC1.getDocument().addDocumentListener(documentListener);
textFieldFencing_LC1.getDocument().putProperty("parent","LC1"); // String, String
注意,putProperty()中的第二个参数是一个对我有意义的String,所以我可以检查LC#。虽然DocumentListener中的更新看起来像这样......
final DocumentEvent.EventType type = documentEvent.getType();
String typeString = null;
// Cast documentEvent to String
txtField.setText( (String) documentEvent.getDocument().getProperty("parent") );
if (type.equals(DocumentEvent.EventType.CHANGE)) {
typeString = "(parent: " + txtField.getText() + ") Change";
} else if (type.equals(DocumentEvent.EventType.INSERT)) {
typeString = "(parent: " + txtField.getText() + ") Insert";
} else if (type.equals(DocumentEvent.EventType.REMOVE)) {
typeString = "(parent: " + txtField.getText() + ") Remove";
}
System.out.print("Type : " + typeString);
final Document source = documentEvent.getDocument();
final int length = source.getLength();
System.out.println("Length: " + length);
}
输出现在看起来像......
Type : (parent: LC1) RemoveLength: 0
Type : (parent: LC1) InsertLength: 1
Type : (parent: LC1) InsertLength: 2
Type : (parent: LC1) InsertLength: 3
底线:我需要监听的JTextField上引用的Class和两行代码,因此发生自动更新,比在所有这些字段上添加CaretListener要好得多。