Don't know how to fix my PropertyChangeListener on a JFormattedTextField

孤人 提交于 2019-12-22 11:27:54

问题


EDIT at end of post

Test Code and Output

import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.text.NumberFormatter;

public class Test{
    private JFormattedTextField input, input2;
    private NumberFormatter formatter;
    private PropertyChangeListener listener;


    public Test(){
       formatter = new NumberFormatter(NumberFormat.getNumberInstance());
       input = new JFormattedTextField(formatter);
       input2 = new JFormattedTextField(formatter);
       listener = new PropertyChangeListener(){
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                convert(evt);
            }
        };
       input.setColumns(4);
       input2.setColumns(4);
       input.addPropertyChangeListener("value", listener);
       input2.addPropertyChangeListener("value", listener);
       input.setValue(0.0);
       JPanel panel = new JPanel();
       panel.add(input);
       panel.add(input2);


       JOptionPane.showMessageDialog(null, panel);
    }

    private void convert(PropertyChangeEvent evt){
        if (evt.getSource()== input){
            if (evt.getSource()!= null){
                double temp;
                temp = converter((Double)evt.getNewValue());
                input2.setValue(temp);
            }

        }


    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable(){
           public void run(){
              new Test(); 
           } 
        });

    }

    private double converter(double value){
        value = value*2;

        return value;
    }

}

The stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
        at test.Test.convert(Test.java:46)

My thoughts

Because I have a method that passes in a double (it's convert()) , and seemingly evt.getNewValue() returns the direct value, which at the time of input is technically a long, it's throwing that error.

But every attempt at parsing my evt.getNewValue() to a double hasn't worked. Perhaps a little knowledge of what I'm trying to do with this program would help.


What The Program is for

So I've got a JPanel (in a tabbedPane) that has two JFormattedTextField inputs. It's a conversion application. My conversion class method passes in a double and returns a double. I'd like the fields to be linked together, or in other words, as soon as one field's input is changed the other changes with it (as in it's the output of the conversion).

I was considering scrapping the PropertyChangListener and going for a DocumentListener instead, but opted to try the former first as the latter has 3 overrideable methods I have to take care of, one of which might cause some unexpected results (highlighting and deleting the field would trigger two events for example).


TL;DR:

Is there a better way of getting a dynamically updating, dual input field application? Input one number into one field and the other field's number automatically updates.

Still a novice at Java.


Edit1

I've found a temporary solution: Have a DecimalFormat as the format in the JFormattedTextField. But if it could work without having a decimal as well I'd love it.

Edit2

Question answered, didn't realize evt.getNewValue() was returning a Number instance.


回答1:


All you know for sure is that the object returned by evt.getNewValue() is a Number object. What if you use that information to your advantage and try something along these lines:

temp = ((Number)evt.getNewValue()).doubleValue();


来源:https://stackoverflow.com/questions/22776172/dont-know-how-to-fix-my-propertychangelistener-on-a-jformattedtextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!