0

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");
                      }                         

3 Answers 3

1

You are assigning the variable value at TextFieldHandler. The action performed event of a JTextField will fire at the Enter key event. It will be solved if you assign the variable at the beginning of DoListener.actionPerformed(). So that only thing you need is to move the code in TextFieldHandler.actionPerformed() to DoListener.actionPerformed()

0
0

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.

1
  • thank u! I figured out that if I pasted the String as shown below then it worked perfectly. if (arg.equals("Calculate Insurance")) { String valueStr=insurancevaluetextField.getText(); value=Double.parseDouble(valueStr);
    – Phoenix
    Mar 25, 2014 at 2:25
0

ActionListener event will occur on TextField only when you type something and hit enter.

this handler will not be raised, insurancevaluetextField.addActionListener (handler); its value will be 0, that why the error message was displayed if you dont hit enter after typing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.