Just a snippet of code. Doing a project for class where I have to convert Celsius, Fahrenheit and Kelvin based on a change to their respective JFormattedTextFields
. The problem is that when the listener
reacts to the change in Celsius, my program changes Fahrenheit, which then reacts to my Fahrenheit listener, which reacts my Celsius listener again, and so on. I haven't worked in my Kelvin stuff yet due to this problem. I doubt my code is needed, since this is more of a conceptual problem, but here it is anyway:
private class ValueChangeHandler implements PropertyChangeListener
{
public void propertyChange(PropertyChangeEvent event)
{
Object celsiusChange = event.getSource();
Object fahrenheitChange = event.getSource();
Object kelvinChange = event.getSource();
if(celsiusChange == celsiusField)
{
tempCelsius = (Double)celsiusField.getValue();
tempFahrenheit = celToFah(tempCelsius);
tempKelvin = celToKel(tempCelsius);
fahrenheitField.setValue(tempFahrenheit);
kelvinField.setValue(tempKelvin);
}
else if(fahrenheitChange == fahrenheitField)
{
tempFahrenheit = (Double)fahrenheitField.getValue();
tempCelsius = fahToCel(tempFahrenheit);
tempKelvin = fahToKel(tempFahrenheit);
celsiusField.setValue(tempCelsius);
kelvinField.setValue(tempKelvin);
}
}