You probably need to look at the DocumentListener example here
EDIT:
I know this problem from my first touch of JFormattedTextField , here is an example that does not work on firts focusLost :-) and probably demonstrated your problem
the minimum limit is set to 10.000,- for JFormattedTextField ,
first. JFormattedTextField handling FocusListener (output should be delayed by invokeLater )
second. JFormattedTextField handling DocumentListener (every one works ...)
outward look

here is the same problem, because I'm only 500 here, and nothing has changed on focusLost, the correct amount should be> = 10.000,-

at 2dn. focusLost works ....

I do not know how this is possible, but it is solved by packing in invokeLater() , and then it works on the 1st one. focusLost (you need to uncomment these lines of code)
from code
import java.awt.*; import java.awt.event.*; import java.math.RoundingMode; import java.text.NumberFormat; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; public class FormatterLimit { private JFrame frame = new JFrame(); private JPanel pnl = new JPanel(); private JLabel focusLabel = new JLabel(" focusLost Handle "); private JFormattedTextField formTextField; private JLabel docLabel = new JLabel(" document Handle "); private JFormattedTextField formTextField1; private NumberFormat formTextFieldFormat; private double amount = 10000.00; public FormatterLimit() { formTextFieldFormat = NumberFormat.getNumberInstance(); formTextFieldFormat.setMinimumFractionDigits(2); formTextFieldFormat.setMaximumFractionDigits(2); formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP); focusLabel.setFont(new Font("Serif", Font.BOLD, 14)); focusLabel.setForeground(Color.blue); focusLabel.setPreferredSize(new Dimension(120, 27)); formTextField = new JFormattedTextField(formTextFieldFormat); formTextField.setValue(amount); formTextField.setFont(new Font("Serif", Font.BOLD, 22)); formTextField.setForeground(Color.black); formTextField.setBackground(Color.yellow); formTextField.setPreferredSize(new Dimension(120, 27)); formTextField.setHorizontalAlignment(SwingConstants.RIGHT); formTextField.addFocusListener(new FocusListener() { @Override public void focusGained(FocusEvent e) { formTextField.requestFocus(); formTextField.setText(formTextField.getText()); formTextField.selectAll(); } @Override public void focusLost(FocusEvent e) {
mKorbel
source share