0

I'd like my JTextField, which I use to enter numbers, to have digits grouped (e.g. with a separator char: 1,000,000 or with a little gap 1 000 000).

I'm aware that this can be done using a JFormattedTextField together with NumberFormat.

What I don't like in the Formatter approach is that it actually modifies the text by inserting characters. I'd rather have a special renderer/decorator which customizes the look only.

(An advantage of this would also be that the formatting would apply already while typing text, not only after committing)

Can this be done? By overriding one of the paint methods of JTextField? Which one? Or by using a different Swing text component?

MonitorLizard
  • 147
  • 1
  • 11
  • please why ?, `the formatting applies already while typing text, not only after committing` == it is not true – mKorbel Jan 28 '14 at 12:36
  • see my edit. I clarified: I want it to apply immediately, but it's not happening for JFormattedTextField – MonitorLizard Jan 28 '14 at 12:42
  • here are a few attempts about how to simulating ATM (JTextField/JFormattedTextField), you would need to serch for/customize Editor – mKorbel Jan 28 '14 at 12:46
  • 1
    Use a `JSpinner` as seen [here](http://stackoverflow.com/a/10021773/418556). It will render `9000` as `9,000` automatically according to locale. – Andrew Thompson Jan 28 '14 at 14:18
  • 1
    @AndrewThompson: For JSpinner the problem is exactely the same (it actually uses a JFormattedTextField internally) – MonitorLizard Jan 28 '14 at 15:13

0 Answers0