如何在单击按钮时从 JTextField 中删除 DocumentFilter 文本
How to remove DocumentFilter text from JTextField on button click
class MyDocumentFilter extends DocumentFilter {
@Override
public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
for (int n = string.length(); n > 0; n--) {//an inserted string may be more than a single character i.e a copy and paste of 'aaa123d', also we iterate from the back as super.XX implementation will put last insterted string first and so on thus 'aa123d' would be 'daa', but because we iterate from the back its 'aad' like we want
char c = string.charAt(n - 1);//get a single character of the string
if (Character.isAlphabetic(c) || c == ' ') {//if its an alphabetic character or white space
super.replace(fb, i, i1, String.valueOf(c), as);//allow update to take place for the given character
} else {//it was not an alphabetic character or white space
}
}
}
@Override
public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
super.remove(fb, i, i1);
}
@Override
public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
super.insertString(fb, i, string, as);
}
}
我在 JTextField 中添加了这个。现在我想在单击按钮时清除 JTextField 文本。
这是我填写表格的方式
提前致谢**
我同意@camickr - 任何文本字段在执行 .setText("") 时都会清除。一个 ActionListener 应该就足够了。
clearFieldsButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
txtField.setText("");
...
}
});
正如您在对 camickr 的回复中提到的那样,我只能想到一个原因,TextFields 没有被 .setText("") 清除。
您可能必须使用以下方式刷新 JPanel:
panel1.revalidate();
panel1.repaint();
是因为我在 JTextField 上使用了 DocumentFilter,它没有从 JtextField 中删除文本。将使用 DocumentFilter remove() 方法删除文本
try {
txtFirstName.getDocument().remove(0, txtFirstName.getText().length());
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
class MyDocumentFilter extends DocumentFilter {
@Override
public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
for (int n = string.length(); n > 0; n--) {//an inserted string may be more than a single character i.e a copy and paste of 'aaa123d', also we iterate from the back as super.XX implementation will put last insterted string first and so on thus 'aa123d' would be 'daa', but because we iterate from the back its 'aad' like we want
char c = string.charAt(n - 1);//get a single character of the string
if (Character.isAlphabetic(c) || c == ' ') {//if its an alphabetic character or white space
super.replace(fb, i, i1, String.valueOf(c), as);//allow update to take place for the given character
} else {//it was not an alphabetic character or white space
}
}
}
@Override
public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
super.remove(fb, i, i1);
}
@Override
public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
super.insertString(fb, i, string, as);
}
}
我在 JTextField 中添加了这个。现在我想在单击按钮时清除 JTextField 文本。
提前致谢**
我同意@camickr - 任何文本字段在执行 .setText("") 时都会清除。一个 ActionListener 应该就足够了。
clearFieldsButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
txtField.setText("");
...
}
});
正如您在对 camickr 的回复中提到的那样,我只能想到一个原因,TextFields 没有被 .setText("") 清除。 您可能必须使用以下方式刷新 JPanel:
panel1.revalidate();
panel1.repaint();
是因为我在 JTextField 上使用了 DocumentFilter,它没有从 JtextField 中删除文本。将使用 DocumentFilter remove() 方法删除文本
try {
txtFirstName.getDocument().remove(0, txtFirstName.getText().length());
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}