Search code examples
javastringjtextfield

How do I get a second textfield value to be read and calculated


I have been stuck for days trying to figure out why I cannot get the value from the inurancevaluetextfield. I have attached both files so that if anyone wants to run it they can see my problem. I get to the stage where I select Insurance checkbox and enter a value between 100 and 2000, but I keep getting the error message stating my value is not between those amounts. So I don't think the textfield is being read at all.

    public void setInsurancecost(double insurancecost) 
{
    this.insurancecost = insurancecost;    
}

public double getInsurancecost()
{
    if (value < 100)
    {   JOptionPane.showMessageDialog(null, "Insurance can only be purchased for items valued between $100 - $2000.");
    }
    else if (value >= 100)
    {  insurancecost = value * 0.05;
    }
    else if (value > 2000)
    {   JOptionPane.showMessageDialog(null, "Insurance can only be purchased for items valued between $100 - $2000.");
    }
    return insurancecost;
} 

public class TextFieldHandler implements ActionListener
{
    @Override
    public void actionPerformed (ActionEvent e)
    {
        DecimalFormat df = new DecimalFormat("0.00");

        String valueStr=insurancevaluetextField.getText();
        value=Double.parseDouble(valueStr);
    }
}
 private class DoListener implements ActionListener
{
     @Override
                public void actionPerformed(ActionEvent e)
                {
                  DecimalFormat df = new DecimalFormat("0.00");

                  String arg = e.getActionCommand();
                   if (arg.equals("Calculate Insurance"))
                    {
                     System.out.println( "calculate insurance button selected");

                      add (insuranceDisplayArea);  
                      insuranceDisplayArea.setText("The insurance charge for your package is:  $"+df.format(getInsurancecost())+"\n");
                      }                         

Solution

  • I looked at your code. TheTextFiled handler you have registered with textField only invoked when you press enter key on text field.

        TextFieldHandler handler = new TextFieldHandler();
        textField.addActionListener(handler);
    

    Therefore actionPerformed in TextFieldHandler only invoked if you press enter on textField and that moment only your insurancevaluetextField read and set to value.

    You can either change the listener so that text filed key change will capture value or just add textfiled read logic into calculate button listener. i.e DoListener

    private class DoListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            DecimalFormat df = new DecimalFormat("0.00");
    
            String weightStr = textField.getText();
            weight = Double.parseDouble(weightStr);
    
            String valueStr = insurancevaluetextField.getText();
            value = Double.parseDouble(valueStr);
       .... }
    

    I would suggest the changing DoListener.

    when you are using API read about it. Know what they do exactly. Also debug the program using good IDE. You will spot the error very quickly.