I have a JTable.I have JSpinner class in One of its column.When user click it a instance of JSpiner opens with JSpinnerDateModel.It has two features.One can use Spinner up/down button to change time.And one can write time by typing.I am using am/pm 12-hour format.Now i want that when user type,he is not able to delete colon using backspace.WHile using backspace when colon arrives,cursor moves one step ahead.
public class TimeEditor extends DefaultCellEditor{
private JSpinner timeSpinner;
SpinnerDateModel model;
JSpinner.DateEditor de;
TimeEditor(JFormattedTextField jf){
super(jf);
model = new SpinnerDateModel();
model.setCalendarField(Calendar.MINUTE);
timeSpinner = new JSpinner();
timeSpinner.setModel(model);
de= new JSpinner.DateEditor(timeSpinner, "hh:mm a");
timeSpinner.setEditor(new JSpinner.DateEditor(timeSpinner, "h:mm a"));
timeSpinner.setBorder(null);
}
And the code from where i am calling time editor is
` String mask = "**:*****"; MaskFormatter timeFormatter; JFormattedTextField formattedField; public TestCalculator(){ initComponents(); getContentPane().setBackground(Color.LIGHT_GRAY);
try{
timeFormatter = new MaskFormatter(mask);
timeFormatter.setPlaceholderCharacter(':');
formattedField = new JFormattedTextField(timeFormatter);
}
catch(ParseException e){
e.printStackTrace();
}