Java Utililty Methods JTextField Number

List of utility methods to do JTextField Number

Description

The list of methods to do JTextField Number are organized into topic(s).

Method

voidformat(JTextField txt, double num, String fmt)
format
DecimalFormat df = new DecimalFormat(fmt);
txt.setText(df.format(num));
doublegetNum(JTextField txt, double defaultNum)
get Num
double num = defaultNum;
try {
    String str = txt.getText().trim();
    num = Double.parseDouble(str);
} catch (Exception ex) {
return num;
intgetNumber(JTextField field, Integer defaultInt)
get Number
String value = field.getText();
Integer valueInt = null;
if (value.equals("")) {
    valueInt = defaultInt;
} else {
    try {
        valueInt = Integer.parseInt(value);
    } catch (Exception e) {
...
booleanlargeEqualThan(javax.swing.JTextField input, double x)
Check if the JTextField input is larger or equal than x
double d = new Double(input.getText()).doubleValue();
if (d >= x)
    return true;
return false;
intparseIntFromField(JTextField jField, int pDefault)
parse Int From Field
int result = pDefault;
try {
    result = Integer.parseInt(jField.getText());
} catch (NumberFormatException nfe) {
    jField.setText(Integer.toString(pDefault));
    result = pDefault;
return result;
...
voidplaceDoubleTextInField(JTextField inField, int length, double toPlace)
place Double Text In Field
char[] valArray = Double.toString(toPlace).toCharArray();
inField.setText(new String(Arrays.copyOf(valArray, Math.min(valArray.length, length))));
doublereadDoubleFromField(JTextField inField, double defaultValue)
read Double From Field
try {
    return Double.valueOf(inField.getText());
} catch (Exception e) {
    return defaultValue;
voidsetNumericAndCharOnly(final javax.swing.JTextField textField)
set Numeric And Char Only
textField.addKeyListener(new KeyAdapter() {
    @Override
    public void keyTyped(KeyEvent evt) {
        if (!Character.isLetterOrDigit(evt.getKeyChar())) {
            evt.consume();
});
...
voidsetTextField(javax.swing.JTextField textField, double value, int precision)
Set a text field with the given value using the given precision.
java.text.NumberFormat nf = java.text.NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(precision);
nf.setMinimumFractionDigits(precision);
nf.setGroupingUsed(false);
textField.setText(nf.format(value));
textField.setCaretPosition(0);
voidshowInDecimalFormat(JTextField pJTextField)
show In Decimal Format
pJTextField.setText(formatNumber(getDecimalNumber(pJTextField.getText().trim())));