Frage

How do I prevent entering '.' character in JFormattedTextField (or JTextField if that easier) ?

War es hilfreich?

Lösung

You can also write a simple listener:

addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { if(e.getKeyChar() == '.') { e.consume(); } } });


Edit:

As @HovercraftFullOfEels mentioned in the comments, you better use DocumentFilter, see this answer for a good explanation and refer for the tutorial.

Edit 2:
You can try to MaskFormatter:

MaskFormatter formatter = new MaskFormatter(/*Check out the link*/);
JFormattedTextField tf = new JFormattedTextField(formatter );
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top