Search code examples
javaswingif-statementjtextfieldjcombobox

Equation answer won't output to JTextField with using CoomboBox if statments


I am very new to Java and am taking a Java 1 class. I am trying to use a JComboBox selection in an if statement and then display an answer based on the selection in a JTextField. It all compiled correctly but the answer will not display and I don't Know what i need to change. I've searched and tried several different things but none seemed to work.

import javax.swing.*;
import java.awt.event.*;

public class HayDyGuiTempConv extends JFrame
{

public static void main(String[] args)
{
    new HayDyGuiTempConv();
}

private JButton buttonConvert;
private JButton exitButton;
private JTextField textAmount;
private String fieldText;
private JTextField field;



public HayDyGuiTempConv()
{

    this.setSize(440,150);
    this.setLocation(350,420);
    this.setTitle("Temperature Conversion");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ButtonListener bl = new ButtonListener();
    JPanel panel1 = new JPanel();
    panel1.add(new JLabel("Enter temperature to convert: "));

    textAmount = new JTextField(6);
    panel1.add(textAmount);

    JComboBox<String> comboBox = new JComboBox<> (new String[] {"C to F", "F to C"});
    comboBox.addActionListener(bl);
    panel1.add(comboBox);

    buttonConvert = new JButton("Convert");
    buttonConvert.addActionListener(bl);
    buttonConvert.setToolTipText("Convert the temperature.");
    panel1.add(buttonConvert);

    panel1.add(new JLabel("Temp: "));

    field = new JTextField(6);
    field.setEditable(false);
    panel1.add(field);

    exitButton = new JButton("Exit");
    exitButton.addActionListener(bl);
    exitButton.setToolTipText("Exit the program.");
    panel1.add(exitButton);

    this.add(panel1);

    this.setVisible(true);
}

private class ButtonListener implements ActionListener
{

    public void actionPerformed(ActionEvent e)
    {


        if(e.getSource() == buttonConvert)
        {

            if(e.getSource() == ("C to F"))
            {
                double tempEntered = Double.parseDouble(textAmount.getText());
                double tempConverted = tempEntered - 32 * (5/9);
                String tempAmount = (Double.toString(tempConverted));
                field.setText(tempAmount);
            }
            else if(e.getSource() == ("F to C"))
            {
                double tempEntered = Double.parseDouble(textAmount.getText());
                double tempConverted = tempEntered * (9/5) + 32;
                String tempAmount = (String.format("%.2f",(tempConverted)));
                field.setText(tempAmount);
            }
        }
        else if(e.getSource() == exitButton)
        {
            System.exit(0);
        }
    }
}
}

Edit : I tried both suggestions and with both of them when I type in an number and try to convert I get this in the interactions pane: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JComboBox


Solution

  • if(e.getSource() == ("C to F"))
    

    The "source" of the event is a component, not a String. In this case it is the JComboBox.

    You want to test the selected value of the combo box so the code should be something like:

    JComboBox comboBox = (JComboBox)e.getSource();
    int index = comboBox.getSelectedIndex();
    
    If (index == 0)
        //  do C to F conversion
    else
        //  do F to C conversion
    

    Also, don't use "==" to compare String values. In the future for String comparison you use the equals(...) method of the String.

    Edit:

    I thought you were adding the ActionListener to the combobox. Then you can just automatically do the conversion when the item is selected from the combo box. There is no need for the button.

    If you want to keep the button then you need to define your combo box as an instance variable in your class. Then you can just access the combo box by name.