Java JTextField Number verifyNumber_geq(JTextField field, Long number)

Here you can find the source of verifyNumber_geq(JTextField field, Long number)

Description

Returns true if the number in field is greater or equal the given number.

License

Open Source License

Parameter

Parameter Description
field the text field
number the number

Return

true, iff the value in the field is valid

Declaration

public static boolean verifyNumber_geq(JTextField field, Long number) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;

import javax.swing.BorderFactory;

import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class Main {
    /**//  w ww .j  a  v a 2 s.  co  m
     * Returns true if the number in field is greater or equal the given number. 
     * Also changes the fields layout to give the user a feedback which values aren't valid.
     * @param field the text field
     * @param number the number
     * @return true, iff the value in the field is valid
     */
    public static boolean verifyNumber_geq(JTextField field, Integer number) {
        boolean res;
        try {
            res = Integer.parseInt(field.getText()) >= number;
        } catch (Exception ex) {
            res = false;
        }
        JTextField tmp = new JTextField();
        Border defaultBorder = tmp.getBorder();
        if (!res) {
            // Mark as error

            // default thickness
            int thickness = 1;
            if (tmp.getBorder() instanceof LineBorder) {
                // if the look and feel uses some line border then get the thickness from it
                thickness = ((LineBorder) tmp.getBorder()).getThickness();
            }
            // red line border with the hopefully same thickness as the old border
            Border errorBorder = BorderFactory.createLineBorder(new Color(255, 0, 0), thickness);
            // this is a hack: create a border inside the red border so that the distance from text to our new border seems to be the same as with the original border
            Border insideBorder = BorderFactory.createLineBorder(new Color(255, 238, 238),
                    tmp.getBorder().getBorderInsets(tmp).left - thickness); // light red, color of the JTextField Background
            field.setBackground(new Color(255, 238, 238));
            field.setBorder(BorderFactory.createCompoundBorder(errorBorder, insideBorder));
        } else {
            // this is easier .. set the default border from look&feel
            field.setBackground(UIManager.getColor("TextField.background"));
            field.setBorder(defaultBorder);
        }
        return res;
    }

    /**
     * Returns true if the number in field is greater or equal the given number. 
     * Also changes the fields layout to give the user a feedback which values aren't valid.
     * @param field the text field
     * @param number the number
     * @return true, iff the value in the field is valid
     */
    public static boolean verifyNumber_geq(JTextField field, Long number) {
        boolean res;
        try {
            res = Long.parseLong(field.getText()) >= number;
        } catch (Exception ex) {
            res = false;
        }
        JTextField tmp = new JTextField();
        Border defaultBorder = tmp.getBorder();
        if (!res) {
            // Mark as error

            // default thickness
            int thickness = 1;
            if (tmp.getBorder() instanceof LineBorder) {
                // if the look and feel uses some line border then get the thickness from it
                thickness = ((LineBorder) tmp.getBorder()).getThickness();
            }
            // red line border with the hopefully same thickness as the old border
            Border errorBorder = BorderFactory.createLineBorder(new Color(255, 0, 0), thickness);
            // this is a hack: create a border inside the red border so that the distance from text to our new border seems to be the same as with the original border
            Border insideBorder = BorderFactory.createLineBorder(new Color(255, 238, 238),
                    tmp.getBorder().getBorderInsets(tmp).left - thickness); // light red, color of the JTextField Background
            field.setBackground(new Color(255, 238, 238));
            field.setBorder(BorderFactory.createCompoundBorder(errorBorder, insideBorder));
        } else {
            // this is easier .. set the default border from look&feel
            field.setBackground(UIManager.getColor("TextField.background"));
            field.setBorder(defaultBorder);
        }
        return res;
    }
}

Related

  1. readDoubleFromField(JTextField inField, double defaultValue)
  2. setNumericAndCharOnly(final javax.swing.JTextField textField)
  3. setTextField(javax.swing.JTextField textField, double value, int precision)
  4. showInDecimalFormat(JTextField pJTextField)
  5. validateDoubleInput(Component parentComponent, JLabel label, JTextField textField, String valueDescription, String errorTitle, boolean positiveValue, boolean showMessage, boolean valid)
  6. within(javax.swing.JTextField input, double low, double high, Vector errMsgList, String errMsg)