Here you can find the source of validateDoubleInput(Component parentComponent, JLabel label, JTextField textField, String valueDescription, String errorTitle, boolean positiveValue, boolean showMessage, boolean valid)
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 |
public static boolean validateDoubleInput(Component parentComponent, JLabel label, JTextField textField, String valueDescription, String errorTitle, boolean positiveValue, boolean showMessage, boolean valid)
//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; } }