I have a JFormattedTextField that have a decimal format and if the value is lower or equal 0 it shows a showInputDialog to update it.
How can I set a 'real time' decimal formatter for showInputDialog?
// Format
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
// Text field
JFormattedTextField textField = new JFormattedTextField();
// Validation
String regex = "[.,]";
String valueString = textField.getText().split(",")[0].replaceAll(regex, "");
int value = (int) Double.parseDouble(valueString);
while(value <= 0) {
value = Integer.valueOf(JOptionPane.showInputDialog(parentComponent, "The value must be greater than 0", "Warning", JOptionPane.WARNING_MESSAGE));
}
I can't figure out how can I do it.
NumberFormatter (or MaskFormatter) is probably what you are looking for.