0

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();
      }
  • 3
    you can find some answers [here](http://stackoverflow.com/questions/11215449/is-there-a-way-to-put-a-colon-in-a-jtextfield-so-it-cant-be-removed) – guleryuz Feb 13 '16 at 21:05
  • 1
    [How to Use Formatted Text Fields](http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html) or you could cheat and use something like [this](http://stackoverflow.com/questions/11881301/best-way-to-constrain-user-to-enter-a-time-in-a-jtextfield/11881681#11881681) – MadProgrammer Feb 13 '16 at 21:18
  • I have tried JFormattedTextField. It s not working @MadProgrammer – Anuradha Sharma Feb 13 '16 at 21:29
  • http://stackoverflow.com/questions/11215449/is-there-a-way-to-put-a-colon-in-a-jtextfield-so-it-cant-be-removed – Anuradha Sharma Feb 13 '16 at 21:39
  • @AnuradhaSharma What *exactly* isn't working? "*It is not working*" isn't a very satisfying description... Did you make sure to catch / throw the `ParseException`? If that doesn't help, please consider to edit your answer by posting a [mcve] that is producing the problem. – Lukas Rotter Feb 13 '16 at 21:42
  • @AnuradhaSharma your question is closed, so I added [new answer](http://stackoverflow.com/questions/11215449/is-there-a-way-to-put-a-colon-in-a-jtextfield-so-it-cant-be-removed/35386010#35386010) to other question, hope it helps you. – guleryuz Feb 13 '16 at 22:25

0 Answers0