Java JTextField validateJTextField(JTextField text, Component aThis)

Here you can find the source of validateJTextField(JTextField text, Component aThis)

Description

Validates if the text field contains a number

License

Open Source License

Parameter

Parameter Description
text The jtext component
aThis The other component

Exception

Parameter Description
NumberFormatException The exception that will be launched

Return

A number

Declaration

public static double validateJTextField(JTextField text, Component aThis) throws NumberFormatException 

Method Source Code


//package com.java2s;
/* //from   www. j  a  v  a2  s  .c om
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.awt.Component;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Main {
    /**
     * Validates if the text field contains a number
     *
     * @param text The jtext component
     * @param aThis The other component
     * @return A number
     * @throws NumberFormatException The exception that will be launched
     */
    public static double validateJTextField(JTextField text, Component aThis) throws NumberFormatException {
        try {
            double dev = validateValue(text.getText(), aThis);
            return dev;
        } catch (NumberFormatException ex) {
            text.setText(null);
            throw ex;
        }
    }

    /**
     * Validates if the text is a number
     *
     * @param text The string 
     * @param x The component
     * @return A number
     * @throws NumberFormatException The exception that will be launched.
     */
    public static double validateValue(String text, Component x) throws NumberFormatException {
        double dev = -1;
        try {
            dev = Double.parseDouble(text);
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(x, "Error parsing to number in field: " + text, "Error message",
                    JOptionPane.ERROR_MESSAGE);
            throw ex;
        }
        return dev;
    }
}

Related

  1. showFileOpenDialogAndChangePrefs(String prefName, FileNameExtensionFilter fileNameExtensionFilter, JTextField textField, Class forClass, Component parent)
  2. textAreaChangeListener(JTextField a, final ChangeListener list)
  3. toUpperCase(JTextField field)
  4. triggerTextField(JTextField textField, JCheckBox checkBox)
  5. upperText(JTextField text)