我想在输入JFormattedTextField
中的数字时看到格式化数字。例如,当用户键入124451000时,它必须在屏幕上动态显示为124.451.000,而不是在用户按Tab或Enter键时显示。如何强制JFormattedTextField值动态更改
我希望在输入第四个数字后紧跟第三个数字(例如2.566)。
public class MyDocListener implements DocumentListener{
NumberFormat nf = NumberFormat.getIntegerInstance(Locale.GERMAN);
NumberFormatter nff = new NumberFormatter(nf);
DefaultFormatterFactory factory = new DefaultFormatterFactory(nff);
final String newline = "\n";
public void insertUpdate(DocumentEvent e) {
updateLog(e, "inserted into");
NumberFormat numberFormatter = new DecimalFormat("#,###,###");
// I put this code to change the format.
}
public void removeUpdate(DocumentEvent e) {
updateLog(e, "removed from");
}
public void changedUpdate(DocumentEvent e) {
//Plain text components don't fire these events.
}
public void updateLog(DocumentEvent e, String action) {
Document doc = (Document)e.getDocument();
int changeLength = e.getLength();
displayArea.append(
changeLength + " character" +
((changeLength == 1) ? " " : "s ") +
action + doc.getProperty("name") + "." + newline +
" Text length = " + doc.getLength() + newline);
}
}
嗯,我在另一个类中尝试这种代码,我不能跑,我的JFormattedTextField,我不能在另一个class.I succced无法访问对象,它是另一个类。
嗯,我做到了,但我不能改变其写作时间 – Alify
的NumberFormat的NumberFormatter在JFormattedTextField中文字; public class MyDocumentListener implements DocumentListener public void insertUpdate(DocumentEvent e){ numberFormatter = new DecimalFormat(“#,###,###”); } } myJFormattedTextFrield.getDocument()。addDocumentListener(new MyDocumentListener()); – Alify
谢谢EyeOfTheHawks – Alify