After applying a MaskFormatter onto a JFormattedTextField, then setting that text field to a column in my JTable, the caret started appearing wherever I click. However I want the caret to appear in the left most position of the cell, no matter where in the cell the user clicks. I tried the setCaretPositionMethod, but it did nothing. My guess would be that there is some sort of table listener, which is forcing the caret to move to where the user clicks, if I knew what this listener was, I could Override the feature.
SSCCE:
public class TableExample extends JFrame {
public TableExample() {
add(makeTable());
}
private JTable makeTable() {
Object[][] tableData = {{"","a","b",""}, {"","c","d",""}};
String[] columns = {"column1", "column2", "column3", "dobColumn"};
JTable table = new JTable(tableData, columns);
////DOB column formats to dd/mm/yy
TableColumn dobColumn = table.getColumnModel().getColumn(3);
DateFormat df = new SimpleDateFormat("dd/mm/yy");
JFormattedTextField tf = new JFormattedTextField(df);
tf.setColumns(8);
try {
MaskFormatter dobMask = new MaskFormatter("##/##/##");
dobMask.setPlaceholderCharacter('0');
dobMask.install(tf);
} catch (ParseException ex) {
Logger.getLogger(TableExample.class.getName()).log(Level.SEVERE, null, ex);
}
////Does nothing
tf.setCaretPoisition(0);
dobColumn.setCellEditor(new DefaultCellEditor(tf));
return table;
}
public static void main(String[] args) {
JFrame frame = new TableExample();
frame.setSize( 300, 300 );
frame.setVisible(true);
}
}