Java JTextField Value Get getIntegerFromTextField(JTextField tf, int previousValue)

Here you can find the source of getIntegerFromTextField(JTextField tf, int previousValue)

Description

Gets the integer from text field.

License

Open Source License

Parameter

Parameter Description
tf the tf
previousValue the previous value

Return

the integer from text field

Declaration

public static int getIntegerFromTextField(JTextField tf, int previousValue) 

Method Source Code


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

import java.awt.Color;

import javax.swing.JTextField;

public class Main {
    /**/*from ww w  .  j  ava  2  s .c  o m*/
     * Gets the integer from text field.
     *
     * @param tf
     *            the tf
     * @param previousValue
     *            the previous value
     * @return the integer from text field
     */
    public static int getIntegerFromTextField(JTextField tf, int previousValue) {
        if (tf.getText().length() > 0) {
            try {
                int num = Integer.parseInt(tf.getText());
                tf.setForeground(Color.black);
                return num;
            } catch (NumberFormatException ex) {
                tf.setForeground(Color.red);
            }
        }
        return previousValue;
    }
}

Related

  1. getDoubleTextField(javax.swing.JTextField textField)
  2. getFieldValue(JTextField tf)
  3. getInt(javax.swing.JTextField input)
  4. getInt(JTextField tf)
  5. getInteger(JTextField txt, Integer defaultVal)
  6. getIntValue(JTextField component)
  7. getLabelText(JTextField textField)
  8. getText(JTextField text)
  9. hasValue(JTextField... fields)