【发布时间】:2012-03-12 02:24:19
【问题描述】:
我有一个 DocumentListener 来查找 JTextField 中的任何更改:
public class MyDocumentListener implements DocumentListener {
static String text;
public void insertUpdate(DocumentEvent e) {
updateLog(e);
}
public void removeUpdate(DocumentEvent e) {
updateLog(e);
}
public void changedUpdate(DocumentEvent e) {
//Plain text components do not fire these events
}
public static String passText() {
System.out.println("string that will be passed is: "+text);
return text;
}
public void updateLog(DocumentEvent e) {
Document doc = (Document)e.getDocument();
int length = e.getLength();
try {
text = doc.getText(0, length);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
System.out.println("you typed "+text);
}
}
然后,在另一个类中:
String info = MyDocumentListener.passText();
问题是我只得到一个字符,而不是整个字符串。有什么建议吗?
【问题讨论】:
-
非常简单的一个;)
标签: java swing jtextfield string-length documentlistener