Program seems to be sending the wrong conversions when a temp is entered in the kelvin field. For some reasons changes the kelvin temp value, and also seems to be crashing on random runs when value entered in kelvin field. Also does not return InvalidTemperatureException for the kelvin field. Have tried numerous different things with no avail, any suggestions would be appreciated. Thanks!
public class TempConverter_C_C extends JFrame { double firstValue = 0.0; JPanel topPanel, bottomPanel; JLabel label1, label2, label3; JTextField fahrenheitValueField, celsiusValueField, kelvinValueField; JButton convertButton, clearButton; //constructor TempConverter_C_C() { super("Temp App"); this.setSize(400, 400); this.setLayout(new GridLayout(2,0) );//anonymous layout object this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); //two JPanels topPanel = new JPanel(); bottomPanel = new JPanel(); //set panel layouts topPanel.setLayout(new GridLayout(3,2) ); bottomPanel.setLayout(new FlowLayout() ); //set background colors topPanel.setBackground(Color.BLUE); bottomPanel.setBackground(Color.RED); //add panels to the frame this.add(topPanel); this.add(bottomPanel); //add some components to each panel label1 = new JLabel("Fahrenheit: "); //add it to top panel topPanel.add(label1); //add the field for input fahrenheitValueField = new JTextField(); topPanel.add(fahrenheitValueField); fahrenheitValueField.addFocusListener(new ButtonHandler()); fahrenheitValueField.addActionListener(new ButtonHandler()); label2 = new JLabel("Celsius: "); //add it to top panel topPanel.add(label2); //add the field for input celsiusValueField = new JTextField(); celsiusValueField.addActionListener(new ButtonHandler()); celsiusValueField.addFocusListener(new ButtonHandler()); topPanel.add(celsiusValueField); label3 = new JLabel("Kelvin: "); //add it to top panel topPanel.add(label3); //add the field for input kelvinValueField = new JTextField(); kelvinValueField.addActionListener(new ButtonHandler()); kelvinValueField.addFocusListener(new ButtonHandler()); topPanel.add(kelvinValueField); this.setVisible(true); }//end constructor public static void main(String[] args) { TempConverter_C_C obj = new TempConverter_C_C(); }//end main //implements TempConversionMethods and ActionListener private class ButtonHandler implements ActionListener, TempConversionMethods, FocusListener { //implement the actionPerformed method public void actionPerformed(ActionEvent e) { if(e.getSource().equals(fahrenheitValueField)) { String firstValueString = fahrenheitValueField.getText(); //parse it to a double firstValue = Double.parseDouble(firstValueString); try { if(firstValue < -459.67) { throw new InvalidTemperatureException(firstValue); } celsiusValueField.setText("" + fahrenheitToCelsius(firstValue)); kelvinValueField.setText("" + fahrenheitToKelvin(firstValue)); }//end try catch(InvalidTemperatureException ex) { JOptionPane.showMessageDialog(topPanel, "InvalidTemperatureException:" + " the entered temperature of " + firstValue + " is below absolute zero"); fahrenheitValueField.setText(""); fahrenheitValueField.requestFocus(); }//end catch }//end if if(e.getSource().equals(celsiusValueField)); { String firstValueString = celsiusValueField.getText(); //parse it to a double firstValue = Double.parseDouble(firstValueString); try { if(firstValue < -273.15) { throw new InvalidTemperatureException(firstValue); } fahrenheitValueField.setText("" + celsiusToFahrenheit(firstValue)); kelvinValueField.setText("" + celsiusToKelvin(firstValue)); }//end try catch(InvalidTemperatureException ex) { JOptionPane.showMessageDialog(topPanel, "InvalidTemperatureException:" + " the entered temperature of " + firstValue + " is below absolute zero"); celsiusValueField.setText(""); celsiusValueField.requestFocus(); }//end catch }//end if if(e.getSource().equals(kelvinValueField)) { String firstValueString = kelvinValueField.getText(); //parse it to a double firstValue = Double.parseDouble(firstValueString); try { if(firstValue < 0.0) { throw new InvalidTemperatureException(firstValue); } celsiusValueField.setText("" + kelvinToCelsius(firstValue)); fahrenheitValueField.setText("" + kelvinToFahrenheit(firstValue)); }//end try catch(InvalidTemperatureException ex) { JOptionPane.showMessageDialog(topPanel, "InvalidTemperatureException:" + " the entered temperature of " + firstValue + " is below absolute zero"); kelvinValueField.setText(""); kelvinValueField.requestFocus(); }//end catch }//end if }//end actionPerformed public double celsiusToKelvin(double temp) { return temp + 273.15; }//end method public double kelvinToCelsius(double temp) { return temp - 273.15; }//end method public double celsiusToFahrenheit(double temp) { return 9.0/5.0 * temp + 32; }//end method public double fahrenheitToCelsius(double temp) { return 5.0/9.0 * (temp - 32); }//end method public double fahrenheitToKelvin(double temp) { fahrenheitToCelsius(temp); return celsiusToKelvin(temp); }//end method public double kelvinToFahrenheit(double temp) { kelvinToCelsius(temp); return celsiusToFahrenheit(temp); }//end method public void focusGained(FocusEvent f) { if(f.getSource().equals(fahrenheitValueField)) { fahrenheitValueField.setText(""); } if(f.getSource().equals(celsiusValueField)) { celsiusValueField.setText(""); } if(f.getSource().equals(kelvinValueField)) { kelvinValueField.setText(""); } }//end method public void focusLost(FocusEvent arg0) { }//end method }//end inner class+ public class InvalidTemperatureException extends Exception { //constructor InvalidTemperatureException(double t) { //message to be passed up to exception class super("Invalid temperature value of " + t + " was entered"); firstValue = t; }//end method }//end class }//end class