TableCellEditor for multiple columns I am wanting to use a cell editor and a DocumentFilter to prevent the user from entering certain characters into the table cells.

The columns are as follows:


Product ID
Product Description
Inventory Count
Cost
Minimum Quantity


For Inventory Count, Cost and Minimum Quantity I am going to use a JFormattedTextField.

How can this be improved?

public class ProductTableCellEditor extends AbstractCellEditor implements TableCellEditor
{

    private TableCellEditor editor;

    @Override
    public Object getCellEditorValue()
    {
    if (editor != null) {
        return editor.getCellEditorValue();
    }
    return null;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    {
    JTextField textField = new JTextField();
    textField.setFont(ApplicationStyles.TABLE_FONT);

    switch (column)
    {
    case 0:
        ((AbstractDocument) textField.getDocument()).setDocumentFilter(new ProductTableDocumentFilter());
        editor = new DefaultCellEditor(textField);
        break;
    case 1:
        ((AbstractDocument) textField.getDocument()).setDocumentFilter(new ProductTableDocumentFilter());
        editor = new DefaultCellEditor(textField);
        break;
    case 2:
        throw new UnsupportedOperationException("Not yet supported....");
    case 3:
        throw new UnsupportedOperationException("Not yet supported....");
    case 4:
        throw new UnsupportedOperationException("Not yet supported....");
    default:
        throw new UnsupportedOperationException("Not yet supported....");
    }
    return editor.getTableCellEditorComponent(table, value, isSelected, row, column);
    }
    }
(AbstractDocument) textField.getDocument()


This is bad practice.

I recommend that you do one of these options:


The simple way would be to create a method toAbstractDocument(Document document)
The better way would be to create a field at TextField class such as ? extends Document document.