Java JTextField Value Get getDoubleFromTextField(JTextField textField, double defaultValue)

Here you can find the source of getDoubleFromTextField(JTextField textField, double defaultValue)

Description

get Double From Text Field

License

LGPL

Return

the value in a text field as a double. If the text field contents do not represent a valid double then the default value is inserted into the text field and returned.

Declaration

public static double getDoubleFromTextField(JTextField textField, double defaultValue) 

Method Source Code


//package com.java2s;
/*/* w  w w  . j av  a2s. com*/
 * Utils.java
 *
 * Copyright (c) 2009 JAM Development Team
 *
 * This package is distributed under the Lesser Gnu Public Licence (LGPL)
 *
 */

import javax.swing.*;

public class Main {
    /**
     * @return the value in a text field as a double.
     *         If the text field contents do not represent a valid double then the
     *         default value is inserted into the text field and returned.
     */
    public static double getDoubleFromTextField(JTextField textField, double defaultValue) {

        double value = defaultValue;
        try {
            value = Double.parseDouble(textField.getText());
        } catch (NumberFormatException nfe) {
            textField.setText(defaultValue + "");
        }
        return value;
    }
}

Related

  1. getDouble(javax.swing.JTextField input)
  2. getDoubleTextField(javax.swing.JTextField textField)
  3. getFieldValue(JTextField tf)
  4. getInt(javax.swing.JTextField input)
  5. getInt(JTextField tf)