FocusEvent doesn't get the last value of JFormattedTextField, How I can get it?

前端 未结 3 1711
自闭症患者
自闭症患者 2020-12-02 00:34

I have two JFormattedTextField objects on my JFrame object. I want a basic Math (addition) by the values of these JFormattedTextField

3条回答
  •  鱼传尺愫
    2020-12-02 00:43

    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...
        }
    }
    

提交回复
热议问题