List of utility methods to do JTextField Number
void | format(JTextField txt, double num, String fmt) format DecimalFormat df = new DecimalFormat(fmt);
txt.setText(df.format(num));
|
double | getNum(JTextField txt, double defaultNum) get Num double num = defaultNum; try { String str = txt.getText().trim(); num = Double.parseDouble(str); } catch (Exception ex) { return num; |
int | getNumber(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) { ... |
boolean | largeEqualThan(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; |
int | parseIntFromField(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; ... |
void | placeDoubleTextInField(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)))); |
double | readDoubleFromField(JTextField inField, double defaultValue) read Double From Field try { return Double.valueOf(inField.getText()); } catch (Exception e) { return defaultValue; |
void | setNumericAndCharOnly(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(); }); ... |
void | setTextField(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); |
void | showInDecimalFormat(JTextField pJTextField) show In Decimal Format pJTextField.setText(formatNumber(getDecimalNumber(pJTextField.getText().trim()))); |