JTable Editing for Excel Users
- By Matt Stephens
JTable has always had something of a usability glitch, which I’ve seen several users fall foul of and then complain loudly about. Basically, if you type something into one of the text fields in your JTable, and then click the OK button (thus removing focus from the JTable), the data you typed in simply disappears.
“Gotcha!” thinks the JTable. “Another pesky user who didn’t know how to use my UI properly!”
Of course, the user would understandably have assumed that the mere act of typing something in should cause the system to, like, I don’t know, treat his data with a little bit of care and not simply toss it away.
Worse, if the JTable was in a pop-up dialog and the OK button closed the dialog, then the user wouldn’t know straightaway that his data was lost. He might never find out; except when, for example, the year-end sheets don’t balance, twelve months later, because some of the data never actually made it into the system.
Even worse than that is that the Swing team at Sun would tell you that this isn’t really a bug, it’s a “special feature”: it’s simply the way that JTable is meant to work. Users are supposed to press Enter to commit their edits.
Special feature or not though, it’s a nasty trap: and it’s especially bad that JTable behaves this way by default (always has done).
Luckily, there is a cure. On his SwerveSpot blog, tlperkins describes the problem (less emotively than I did, which is probably a good thing) and serves up the solution:
Simple as dat. Now your JTable can behave just like an Excel user would expect!