如何将 BigDecimal 与 JFormattedTextField 一起使用

发布于 2024-11-08 16:02:25 字数 682 浏览 2 评论 0原文

我有一个字段:

jFormattedTextFieldGrossWeight = new javax.swing.JFormattedTextField();
jFormattedTextFieldGrossWeight.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));

我使用其 setValue() 方法为它分配一个 BigDecimal 值,并允许用户使用此文本字段修改该值。

然后在lostFocus方法中,在线:

jFormattedTextField.commitEdit();
BigDecimal gross = (BigDecimal)this.jFormattedTextFieldGrossWeight.getValue();

我得到以下异常:

java.lang.ClassCastException: java.lang.Long cannot be cast to java.math.BigDecimal

有什么问题吗?我怎样才能修改我的代码来消除这个错误?

I have a field as:

jFormattedTextFieldGrossWeight = new javax.swing.JFormattedTextField();
jFormattedTextFieldGrossWeight.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));

I assign it a BigDecimal value using its setValue() method, and allow the user to modify that value using this textfield.

Then in the lostFocus method, on the line:

jFormattedTextField.commitEdit();
BigDecimal gross = (BigDecimal)this.jFormattedTextFieldGrossWeight.getValue();

I get the following exception:

java.lang.ClassCastException: java.lang.Long cannot be cast to java.math.BigDecimal

Is there anything wrong? How can I modify my code to get rid of this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰ツ都不明白 2024-11-15 16:02:25

你可以试试这个:

JFormattedTextField ftf = new JFormattedTextField();
ftf.setFormatterFactory(new DefaultFormatterFactory(
                        new NumberFormatter(new DecimalFormat("#,##0.00"))));


// Input = 1245678.57
// After the format it will be:
// 1,245,678.57
// So, we need to get rid of the comma's:
String number = ftf.getText().replace(",","");
BigDecimal bd = new BigDecimal(number);

You can try this:

JFormattedTextField ftf = new JFormattedTextField();
ftf.setFormatterFactory(new DefaultFormatterFactory(
                        new NumberFormatter(new DecimalFormat("#,##0.00"))));


// Input = 1245678.57
// After the format it will be:
// 1,245,678.57
// So, we need to get rid of the comma's:
String number = ftf.getText().replace(",","");
BigDecimal bd = new BigDecimal(number);
饮湿 2024-11-15 16:02:25

我已经实现了基于 JFormattedTextField 的数字字段。

JRealNumberField 和 JLocalizedRealNumberField 是 BigDecimal 的文本字段。

它们还支持最小值和最大值。

也许您发现它们很有用(该库是开源的):

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JRealNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JDoubleField.html< /a>

http://softsmithy.sourceforge。 net/lib/docs/api/org/softsmithy/lib/swing/JFloatField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedRealNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/ softsmithy/lib/swing/JLocalizedDoubleField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedFloatField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JWholeNumberField.html< /a>

http://softsmithy.sourceforge。 net/lib/docs/api/org/softsmithy/lib/swing/JByteField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JIntegerField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/ softsmithy/lib/swing/JLongField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JShortField.html

教程:

http://softsmithy.sourceforge.net/lib/docs/tutorial/swing/number/index.html

主页:

< a href="http://www.softsmithy.org" rel="nofollow">http://www.softsmithy.org

下载:

http://sourceforge.net/projects/softsmithy/files/softsmithy/

Maven:

<dependency>  
    <groupid>org.softsmithy.lib</groupid>  
    <artifactid>lib-core</artifactid>  
    <version>0.1</version>  
</dependency>  

-Puce

I've implemented number fields based on JFormattedTextField.

JRealNumberField and JLocalizedRealNumberField are text fields for BigDecimal.

They also support a min and a max value.

Maybe you find them useful (the library is open source):

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JRealNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JDoubleField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JFloatField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedRealNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedDoubleField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedFloatField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JWholeNumberField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JByteField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JIntegerField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLongField.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JShortField.html

Tutorial:

http://softsmithy.sourceforge.net/lib/docs/tutorial/swing/number/index.html

Homepage:

http://www.softsmithy.org

Download:

http://sourceforge.net/projects/softsmithy/files/softsmithy/

Maven:

<dependency>  
    <groupid>org.softsmithy.lib</groupid>  
    <artifactid>lib-core</artifactid>  
    <version>0.1</version>  
</dependency>  

-Puce

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文