Mam dwa obiekty JFormattedTextField
na moim obiekcie JFrame
. Chcę podstawowej matematyki (dodatek) przez wartości tych obiektów JFormattedTextField
. Chcę, aby to się stało, gdy fokus utracił albo pierwsze, albo drugie pole tekstowe. Ale gdy "focusLost()
", zdarzenie nie otrzyma ostatniej wartości, otrzyma poprzednią wartość.FocusEvent nie otrzymuje ostatniej wartości JFormattedTextField, jak mogę ją uzyskać?
Na przykład; tf1
ma 0, a tf2
ma 0 na początku. Piszę 2 do tf1
, a kiedy focusLost()
, wynik (tf1+tf2
) stają się jeszcze 0. kiedy zmiany któregokolwiek z nich, wynik staje 2 (poprzednia wartość)
Jak mogę dostać ostatnie wartości na focusLost?
Oto mój kod:
JFormattedTextField tf1,tf2;
NumberFormat format=NumberFormat.getNumberInstance();
tf1=new JFormattedTextField(format);
tf1.addFocusListener(this);
tf2=new JFormattedTextField(format);
tf2.addFocusListener(this);
i 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)));
}
dobry wątek, istnieją trzy możliwe cechy, dokument i fokus Słuchacza +1 – mKorbel
+1 Myślę, że to jest lepsze podejście. – trashgod