[英]jFormatedTextField currency format on real time user input
This is my code: 这是我的代码:
// create the formatters, default, display, edit
NumberFormatter defaultFormatter = new NumberFormatter(new DecimalFormat("#.##"));
NumberFormatter displayFormatter = new NumberFormatter(new DecimalFormat("#.##€"));
NumberFormatter editFormatter = new NumberFormatter(new DecimalFormat("#.##"));
// set their value classes
defaultFormatter.setValueClass(Double.class);
displayFormatter.setValueClass(Double.class);
editFormatter.setValueClass(Double.class);
// create and set the DefaultFormatterFactory
DefaultFormatterFactory valueFactory = new DefaultFormatterFactory(defaultFormatter,displayFormatter,editFormatter);
jFormattedTextField4.setFormatterFactory(valueFactory);
jFormattedTextField1.setFormatterFactory(valueFactory);
I'm getting issues when the user input a value with a point, like (2.33) , the formatedtextfield change to (2€) and this is not suppose to, because the user entered a valid value. 当用户输入带有点的值时,我遇到问题,如(2.33),格式化文本字段更改为(2€),这不是假设,因为用户输入了有效值。 If the user input (2,33) the result is (2,33) and again is not suppose to, because should be (2.33)
如果用户输入(2,33)结果为(2,33)并且再次没有假设,因为应该是(2.33)
Basically i always want the format (X.XX). 基本上我总是想要格式(X.XX)。 Example:
例:
input vs what i want: 2-2.00€ / 2,22-2.22€ / 312.54-312.54€ / 432-432.00€ / 2,2-2.20€ 输入与我想要的:2-2.00€/ 2,22-2.22€/ 312.54-312.54€/ 432-432.00€/ 2,2-2.20€
EDITED: one week ago this was working, after i mount this project in another windows the issue appear. 编辑:一周前,这是有效的,在我将这个项目安装到另一个窗口后,问题出现了。 At this time i have a portuguese keyboard.
这时我有一个葡萄牙语键盘。 Is it possible that the issue is involved with any windows or system property?
该问题可能与任何窗口或系统属性有关吗?
Edited:I still looking for help. 编辑:我仍在寻求帮助。 Anyone???
任何人??? :s
:■
you have to use getCurrencyInstance() , instead of plain vanilla DecimalFormat, be sure something went wrong in your description (" one week ago this was working, after i mount this project in another windows the issue appear....
") 你必须使用getCurrencyInstance() ,而不是简单的vanilla DecimalFormat,请确保你的描述中出现问题(“
one week ago this was working, after i mount this project in another windows the issue appear....
”)
and/or there you can to define Formatter restricted by the Locale 和/或在那里你可以定义受Locale限制的Formatter
I wouldn't be to use CurrencyInstance
, isn't nice for me, there is possible to force real shorthand name for (eg Euro
= EUR
) by using NavigationFilter
and chars position in row with setDot
/ moveDot
, code example by @camickr 我不会使用
CurrencyInstance
,对我来说不是很好,有可能通过使用NavigationFilter
和chars在行中使用setDot
/ moveDot
来强制使用真正的简写名称(例如Euro
= EUR
), 代码示例来自@camickr
here are simple standards, defaults thats you required, 这里有简单的标准,默认是你需要的,
import java.awt.*;
import java.awt.font.TextAttribute;
import java.math.*;
import java.text.*;
import java.util.Map;
import javax.swing.*;
import javax.swing.JFormattedTextField.*;
import javax.swing.event.*;
import javax.swing.text.InternationalFormatter;
public class DocumentListenerAdapter {
public DocumentListenerAdapter() {
JFrame frame = new JFrame("AbstractTextField Test");
final JFormattedTextField textField1 = new JFormattedTextField(new Double(10.01));
textField1.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
NumberFormat format = DecimalFormat.getCurrencyInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
//formatter.setMinimum(0.0);
//formatter.setMaximum(1000.00);
return formatter;
}
});
final Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
final JFormattedTextField textField2 = new JFormattedTextField(new Double(10.01));
textField2.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(3);
format.setMaximumFractionDigits(3);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
//formatter.setMinimum(0.0);
//formatter.setMaximum(1000.00);
return formatter;
}
});
textField2.getDocument().addDocumentListener(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) {
DocumentEvent.EventType type = documentEvent.getType();
double t1a1 = (((Number) textField2.getValue()).doubleValue());
if (t1a1 > 1000) {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font(attributes));
textField2.setForeground(Color.red);
}
};
SwingUtilities.invokeLater(doRun);
} else {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font("Serif", Font.BOLD, 16));
textField2.setForeground(Color.black);
}
};
SwingUtilities.invokeLater(doRun);
}
}
});
//https://stackoverflow.com/a/20008786/714968
JFormattedTextField jftf2 = new JFormattedTextField();
final InternationalFormatter fmt = new InternationalFormatter(
new MessageFormat("{0,number,000}-{1,number,0000}"));
jftf2.setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory() {
@Override
public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
return fmt;
}
});
jftf2.setValue(new Object[]{111, 1234});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textField1, BorderLayout.NORTH);
frame.add(textField2, BorderLayout.CENTER);
frame.add(jftf2, BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
DocumentListenerAdapter main = new DocumentListenerAdapter();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.