import javax.swing.*;


/**
 * Class for text field holding only doubles.
 */
public class DoubleTextField extends JTextField {
    // Construct field with initial value init with cols columns
    public DoubleTextField(double init, int cols) {
        super("" + init, cols);
    }

    // Construct field with initial value init
    public DoubleTextField(double init) {
        super("" + init);
    }

    // Construct field with initial value 0
    public DoubleTextField() {
        super("0");
    }

    // set value of field to newVal
    public void setDoubleValue(double newVal) {
        setText("" + newVal);
    }

    // retrieve value in field.  Returns 0 if not a double.
    public double getDoubleValue() {
        double doubleVal = 0;

        try {
            doubleVal = Double.valueOf(getText()).doubleValue();
        } catch (NumberFormatException e) {
            setText("Bad input - enter valid double!");
        }

        return doubleVal;
    }
}
