I’m flabbergasted. The Swing folks at Sun have finally come up with a text field component that isn’t completely awful. JFormattedTextField is designed to help developers handle fields that represent typed values: dates, currencies, etc. And it actually manages to do this in a pretty reasonable way, by plugging in a simple formatter class, with and emphasis on simple, which distinguishes it from all the other model interfaces in Swing. But that’s not all!

They even go the extra step for you and provide formatters for dates and numbers. This is something that Sun doesn’t do often, usually leaving the work of making Swing useful to various third-party tool and framework vendors. Remember JavaBeans and the way they were going to lead to a global marketplace of interoperable widgets? Yeah, I’m trying to forget, too.

But the really sweet thing about JFormattedTextField is the way it beats JTextField as a general-purpose text editing component. The biggest problem with JTextField is that it follows the pattern of all the other Swing text components, which means that it is backed by a huge model class. Swing Documents are useful when you’re building a word processor or other large text editor, but it’s complete overkill when you just want to edit a single string. But since JFormattedTextField is designed to use an arbitrary object as its model, you can conveniently sidestep the crushing conceptual weight that other text components place on you. It even fires a nice, normal property-change event when you type in the field, which solves my #1 problem with JTextField: you never know what the hell has happened to it unless you stick a pile of key, focus, and document listeners all over it.

It almost seems like Sun has finally started giving up on the racket that they’ve had going for years with tool vendors, in which they keep putting over-complicated and under-functional frameworks into the core distribution, to ensure that application developers who actually want to get some work done will be forced to buy expensive IDEs and application servers. Hopefully, they’ll keep this up: I might get to like this “ease of development” kick they’re on.