JFormattedTextField 值与我设置的不同
JFormattedTextField value is different then what I have set
我正在使用 JFormattedTextField 在文本字段中显示百分比值,如下所示:
NumberFormat percentDisplayFormat = NumberFormat.getPercentInstance();
NumberFormat editFormat = NumberFormat.getNumberInstance();
percentDisplayFormat.setMinimumFractionDigits(2);
NumberFormatter percentEditFormatter = new NumberFormatter(editFormat);
JFormattedTextField jftfPercentage = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter(percentDisplayFormat), new NumberFormatter(percentDisplayFormat), percentEditFormatter));
jftfPercentage.setBounds(113, 95, 236, 21);
jftfPercentage.setName("JftfPercentage");
并设置如下值:
jftfPercentage.setValue(<someDoubleValue>);
现在,假设我将值设置为 80,那么它显示的值为 8000%。
但是,每当我单击该字段时,它仅显示为 80。
谁能帮我显示我们设置的相同值。提前致谢
我遇到了这个问题。在内部,默认数字格式化程序在显示时将给定值乘以 100。 (如果我们使用 PercentInstance)
NumberFormat percentDisplayFormat = NumberFormat.getPercentInstance();
NumberFormat editFormat = NumberFormat.getNumberInstance();
percentDisplayFormat.setMinimumFractionDigits(2);
NumberFormatter percentEditFormatter = new NumberFormatter(editFormat);
// This was the new change that I have made
NumberFormatter percentDisplayFormatter = new NumberFormatter(percentDisplayFormat)
{
@Override
public String valueToString(Object o) throws ParseException
{
Double number = null;
if (o != null)
{
number = Double.valueOf(o.toString());
number /= 100;
}
// In super class, it multiplies the number by 100. So as we are dividing the number by 100 here. In super level it will become the same value that we have given
return super.valueToString(number);
}
};
JFormattedTextField jftfPercentage = new JFormattedTextField(new DefaultFormatterFactory(percentDisplayFormatter, percentDisplayFormatter, percentEditFormatter));
jftfPercentage.setBounds(113, 95, 236, 21);
jftfPercentage.setName("JftfPercentage");
我正在使用 JFormattedTextField 在文本字段中显示百分比值,如下所示:
NumberFormat percentDisplayFormat = NumberFormat.getPercentInstance();
NumberFormat editFormat = NumberFormat.getNumberInstance();
percentDisplayFormat.setMinimumFractionDigits(2);
NumberFormatter percentEditFormatter = new NumberFormatter(editFormat);
JFormattedTextField jftfPercentage = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter(percentDisplayFormat), new NumberFormatter(percentDisplayFormat), percentEditFormatter));
jftfPercentage.setBounds(113, 95, 236, 21);
jftfPercentage.setName("JftfPercentage");
并设置如下值:
jftfPercentage.setValue(<someDoubleValue>);
现在,假设我将值设置为 80,那么它显示的值为 8000%。
但是,每当我单击该字段时,它仅显示为 80。
谁能帮我显示我们设置的相同值。提前致谢
我遇到了这个问题。在内部,默认数字格式化程序在显示时将给定值乘以 100。 (如果我们使用 PercentInstance)
NumberFormat percentDisplayFormat = NumberFormat.getPercentInstance();
NumberFormat editFormat = NumberFormat.getNumberInstance();
percentDisplayFormat.setMinimumFractionDigits(2);
NumberFormatter percentEditFormatter = new NumberFormatter(editFormat);
// This was the new change that I have made
NumberFormatter percentDisplayFormatter = new NumberFormatter(percentDisplayFormat)
{
@Override
public String valueToString(Object o) throws ParseException
{
Double number = null;
if (o != null)
{
number = Double.valueOf(o.toString());
number /= 100;
}
// In super class, it multiplies the number by 100. So as we are dividing the number by 100 here. In super level it will become the same value that we have given
return super.valueToString(number);
}
};
JFormattedTextField jftfPercentage = new JFormattedTextField(new DefaultFormatterFactory(percentDisplayFormatter, percentDisplayFormatter, percentEditFormatter));
jftfPercentage.setBounds(113, 95, 236, 21);
jftfPercentage.setName("JftfPercentage");