File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes MaskFormatter in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MaskFormatter in JTable" Watch "MaskFormatter in JTable" New topic

MaskFormatter in JTable

Vege Thokr

Joined: Feb 04, 2005
Posts: 3
Hi All,
I'm trying to use Maskformatter in a JTable's column. I went thro' Swing tutorial and coded accordingly.
When I edit values in the table, the formatter does work and restrict the values.
But when the focus is lost, the entered value in the cell disappears.
When I debugged, I found it fails during commitEdit (precisely during stringToValue conversion). Here is the code.
please provide your thoughts.

public class CustomerIDEditor extends DefaultCellEditor {
JFormattedTextField ftf;

public CustomerIDEditor() {
super(new JFormattedTextField());
ftf = (JFormattedTextField)getComponent();
MaskFormatter formatter = null;

try {
formatter = new MaskFormatter("########");
} catch (java.text.ParseException e) {
System.err.println("formatter is bad: " + e.getMessage());

ftf.setFormatterFactory(new DefaultFormatterFactory(formatter));

public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected,
int row, int column) {
JFormattedTextField ftf =
table, value, isSelected, row, column);
System.out.println("getTableCellEditorComponent called: " + value);
return ftf;

public Object getCellEditorValue() {
JFormattedTextField ftf = (JFormattedTextField)getComponent();
try { //The text is valid,
ftf.commitEdit(); //so use it.
} catch (java.text.ParseException e) {
System.out.println("Exception:" + e.getMessage() + "Text:" + ftf.getText() ); }

System.out.println("getCellEditorValue called: " + ftf.getValue());
return ftf.getValue();
I agree. Here's the link:
subject: MaskFormatter in JTable
It's not a secret anymore!