Tengo dos objetos JFormattedTextField
en mi objeto JFrame
. Quiero una Matemática básica (adición) por los valores de estos objetos JFormattedTextField
. Quiero que suceda cuando el foco perdió el primero o el segundo campo de texto. Pero cuando "focusLost()
", el evento no obtiene el último valor, obtiene el valor anterior.FocusEvent no obtiene el último valor de JFormattedTextField, ¿cómo puedo obtenerlo?
Por ejemplo; tf1
tiene 0 y tf2
tiene 0 al principio. Escribo 2 a tf1
, y cuando focusLost()
, resultado (tf1+tf2
) se vuelven aún 0. cuando cambie cualquiera de ellos, el resultado se convierte en 2 (el valor anterior)
¿Cómo consigo los últimos valores en focusLost?
Aquí está mi código:
JFormattedTextField tf1,tf2;
NumberFormat format=NumberFormat.getNumberInstance();
tf1=new JFormattedTextField(format);
tf1.addFocusListener(this);
tf2=new JFormattedTextField(format);
tf2.addFocusListener(this);
y 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)));
}
buen hilo, hay tres posibles de Listeners Property, Document y Focus +1 – mKorbel
+1 Creo que este es el mejor enfoque. – trashgod