我知道这是一个常见问题,但我正在尝试创建一个只接受int数字的TextField,它几乎完成了,这是代码:
创建文本字段:
nome = new JFormattedTextField();
nome.setHorizontalAlignment(SwingConstants.CENTER);
nome.setColumns(2);
DocumentFilter filtro = new FiltroNumero();
((AbstractDocument) nome.getDocument()).setDocumentFilter(filtro);
panel.add(nome);
DocummentFilter:
public class FiltroNumero extends DocumentFilter{
public void insertString(DocumentFilter.FilterBypass fb, int offset, int length,
String text, javax.swing.text.AttributeSet attr)
throws BadLocationException {
fb.insertString(offset, text.replaceAll("[^-0-9]", ""), attr);
}
}
这样,TextField只接受数字和“ - ”,但这意味着“1-”是一个可能的值。
我需要的是一种方法,使文本字段不接受第一个字符后的减号。
如果有人可以帮助我,我会很高兴的。)
答案 0 :(得分:3)
您可以简单地从Document
(在替换之前)获取整个文本,然后从该文档文本创建一个新的String,添加text
参数。然后检查一个匹配整数(负数或正数)的完整正则表达式。如果匹配,则执行替换。类似的东西:
@Override
public void replace(FilterBypass fb, int offs, int length,
String str, AttributeSet a) throws BadLocationException {
String text = fb.getDocument().getText(0,
fb.getDocument().getLength());
StringBuilder builder = new StringBuilder(text);
builder.insert(offs, str);
String newText = builder.toString();
// check
System.out.println("text = " + text
+ ", offset = " + offs
+ ", newText = " + newText);
if (newText.matches("(-)?\\d*")) {
super.replace(fb, offs, length, str, a);
} else {
Toolkit.getDefaultToolkit().beep();
}
}
注意:您应该使用replace
而不是insertString
,但不会因为覆盖它们而受到伤害。
这是一个完整的演示
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class FilterDemo {
public FilterDemo() {
JFrame frame = new JFrame();
frame.add(createFilteredField());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JTextField createFilteredField() {
JTextField field = new JTextField(10);
AbstractDocument document = (AbstractDocument) field.getDocument();
document.setDocumentFilter(new DocumentFilter() {
@Override
public void replace(FilterBypass fb, int offs, int length,
String str, AttributeSet a) throws BadLocationException {
String text = fb.getDocument().getText(0,
fb.getDocument().getLength());
StringBuilder builder = new StringBuilder(text);
builder.insert(offs, str);
String newText = builder.toString();
// check
System.out.println("text = " + text
+ ", offset = " + offs
+ ", newText = " + newText);
if (newText.matches("(-)?\\d*")) {
super.replace(fb, offs, length, str, a);
} else {
Toolkit.getDefaultToolkit().beep();
}
}
@Override
public void insertString(FilterBypass fb, int offs, String str,
AttributeSet a) throws BadLocationException {
String text = fb.getDocument().getText(0,
fb.getDocument().getLength());
StringBuilder builder = new StringBuilder(text);
builder.insert(offs, str);
String newText = builder.toString();
// checks
System.out.println("text = " + text
+ ", offset = " + offs
+ ", newText = " + newText);
if (newText.matches("(-)?\\d*")) {
super.insertString(fb, offs, str, a);
} else {
Toolkit.getDefaultToolkit().beep();
}
}
});
return field;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FilterDemo();
}
});
}
}