FocusEvent没有得到JFormattedTextField的最后一个值,我怎么能得到它?(FocusEvent doesn't get the last value of JForm

编程入门 行业动态 更新时间:2024-10-08 08:26:37
FocusEvent没有得到JFormattedTextField的最后一个值,我怎么能得到它?(FocusEvent doesn't get the last value of JFormattedTextField, How I can get it?)

我的JFrame对象上有两个JFormattedTextField对象。 我想通过这些JFormattedTextField对象的值来获得基本的数学(加法)。 当焦点丢失第一个或第二个文本字段时,我希望它发生。 但是,当“ focusLost() ”事件没有获得最后一个值时,它会获得前一个值。

例如; tf1有0, tf2有0。 我写2到tf1 ,当我改变它们中的任何一个时, focusLost() ,result( tf1+tf2 )变为0时,结果变成2(前一个值)

我如何获得focusLost的最后值?

这是我的代码:

JFormattedTextField tf1,tf2; NumberFormat format=NumberFormat.getNumberInstance(); tf1=new JFormattedTextField(format); tf1.addFocusListener(this); tf2=new JFormattedTextField(format); tf2.addFocusListener(this);

和focusLost() :

public void focusLost(FocusEvent e) { if(tf1.getValue() == null) tf1.setValue(0); if(tf2.getValue() == null) tf2.setValue(0); //because if I dont set, it throws nullPointerException for tf.getValue() BigDecimal no1 = new BigDecimal(tf1.getValue().toString()); BigDecimal no2 = new BigDecimal(tf2.getValue().toString()); System.out.println("total: " + (no1.add(no2))); }

I have two JFormattedTextField objects on my JFrame object. I want a basic Math (addition) by the values of these JFormattedTextField objects. I want it happen when focus lost either the first or the second textfield. But when "focusLost()", event doesn't get the last value, it gets the previous value.

For example; tf1 has 0 and tf2 has 0 at first. I write 2 to tf1, and when focusLost(), result (tf1+tf2) become still 0. when I change any of them, the result becomes 2 (the previous value)

How do I get the last values on focusLost?

Here is my code:

JFormattedTextField tf1,tf2; NumberFormat format=NumberFormat.getNumberInstance(); tf1=new JFormattedTextField(format); tf1.addFocusListener(this); tf2=new JFormattedTextField(format); tf2.addFocusListener(this);

and focusLost():

public void focusLost(FocusEvent e) { if(tf1.getValue() == null) tf1.setValue(0); if(tf2.getValue() == null) tf2.setValue(0); //because if I dont set, it throws nullPointerException for tf.getValue() BigDecimal no1 = new BigDecimal(tf1.getValue().toString()); BigDecimal no2 = new BigDecimal(tf2.getValue().toString()); System.out.println("total: " + (no1.add(no2))); }

最满意答案

我想你应该使用PropertyChangeListener ,请参阅如何编写属性更改侦听器 。

有一个使用JFormattedTextField的例子:

//...where initialization occurs: double amount; JFormattedTextField amountField; ... amountField.addPropertyChangeListener("value", new FormattedTextFieldListener()); ... class FormattedTextFieldListener implements PropertyChangeListener { public void propertyChanged(PropertyChangeEvent e) { Object source = e.getSource(); if (source == amountField) { amount = ((Number)amountField.getValue()).doubleValue(); ... } ...//re-compute payment and update field... } }

I think you should use a PropertyChangeListener, see How to Write a Property Change Listener.

There is an example using JFormattedTextField:

//...where initialization occurs: double amount; JFormattedTextField amountField; ... amountField.addPropertyChangeListener("value", new FormattedTextFieldListener()); ... class FormattedTextFieldListener implements PropertyChangeListener { public void propertyChanged(PropertyChangeEvent e) { Object source = e.getSource(); if (source == amountField) { amount = ((Number)amountField.getValue()).doubleValue(); ... } ...//re-compute payment and update field... } }

更多推荐

本文发布于:2023-07-30 15:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338804.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:到它   我怎么能   FocusEvent   JFormattedTextField

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!