Java JTextField Number validateDoubleInput(Component parentComponent, JLabel label, JTextField textField, String valueDescription, String errorTitle, boolean positiveValue, boolean showMessage, boolean valid)

Here you can find the source of validateDoubleInput(Component parentComponent, JLabel label, JTextField textField, String valueDescription, String errorTitle, boolean positiveValue, boolean showMessage, boolean valid)

Description

Validate double input.

License

Apache License

Parameter

Parameter Description
parentComponent the parent component
label the label of the input
textField the text field containing the input
valueDescription the description of the input
errorTitle the error title
positiveValue if true, only positive values will pass the filter
showMessage if true, a message will be shown if the validation fails
valid the status of previous validations

Return

true of the field is validated, false if not (or if valid is false)

Declaration

public static boolean validateDoubleInput(Component parentComponent,
        JLabel label, JTextField textField, String valueDescription,
        String errorTitle, boolean positiveValue, boolean showMessage,
        boolean valid) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import javax.swing.JTextField;

public class Main {
    /**/*from  www  .  j a  va  2s .co  m*/
     * Validate double input.
     *
     * @param parentComponent the parent component
     * @param label the label of the input
     * @param textField the text field containing the input
     * @param valueDescription the description of the input
     * @param errorTitle the error title
     * @param positiveValue if true, only positive values will pass the filter
     * @param showMessage if true, a message will be shown if the validation
     * fails
     * @param valid the status of previous validations
     * @return true of the field is validated, false if not (or if valid is
     * false)
     */
    public static boolean validateDoubleInput(Component parentComponent,
            JLabel label, JTextField textField, String valueDescription,
            String errorTitle, boolean positiveValue, boolean showMessage,
            boolean valid) {

        label.setForeground(Color.BLACK);
        label.setToolTipText(null);

        // check that a value is specified
        if (textField.getText() == null
                || textField.getText().trim().equals("")) {
            if (showMessage) {
                JOptionPane
                        .showMessageDialog(parentComponent,
                                "You need to specify the "
                                        + valueDescription + ".",
                                errorTitle, JOptionPane.WARNING_MESSAGE);
            }
            valid = false;
            label.setForeground(Color.RED);
            label.setToolTipText("Please select the " + valueDescription);
        }

        double tempValue = -1;

        try {
            tempValue = Double.parseDouble(textField.getText().trim());
        } catch (NumberFormatException nfe) {
            // unparseable double!
            if (showMessage && valid) {
                JOptionPane.showMessageDialog(parentComponent,
                        "You need to specify a number for "
                                + valueDescription + ".", errorTitle,
                        JOptionPane.WARNING_MESSAGE);
            }
            valid = false;
            label.setForeground(Color.RED);
            label.setToolTipText("Please select a number");
        }

        // and it should be zero or more
        if (positiveValue && tempValue < 0) {
            if (showMessage && valid) {
                JOptionPane.showMessageDialog(parentComponent,
                        "You need to specify a positive number for "
                                + valueDescription + ".", errorTitle,
                        JOptionPane.WARNING_MESSAGE);
                textField.requestFocus();
            }
            valid = false;
            label.setForeground(Color.RED);
            label.setToolTipText("Please select a positive number");
        }

        return valid;
    }
}

Related

  1. placeDoubleTextInField(JTextField inField, int length, double toPlace)
  2. readDoubleFromField(JTextField inField, double defaultValue)
  3. setNumericAndCharOnly(final javax.swing.JTextField textField)
  4. setTextField(javax.swing.JTextField textField, double value, int precision)
  5. showInDecimalFormat(JTextField pJTextField)
  6. verifyNumber_geq(JTextField field, Long number)
  7. within(javax.swing.JTextField input, double low, double high, Vector errMsgList, String errMsg)