-1

I am new to Swing and I am trying to disable jspinner, so that it will not be editable.

I added a spinner and change the model type to date in the properties. So that it can display the date&time. Have changed the variable name to "Datespin".

I am trying with below code, but that dint help.

enter image description here

My Question: Can you please help me in making this field non-editable? It should display the date&time when executed, but it shouldn't be editable by the user.

 private void hDateSpinnerPropertyChange(java.beans.PropertyChangeEvent evt) {                                            
        JSpinner spinner= new JSpinner();
        JFormattedTextField spin=((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();
        spin.setEditable(false);

Where am I going wrong?

sady
  • 301
  • 1
  • 7
  • 18
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 29 '16 at 08:13
  • @AndrewThompson Apologize, I have updated screenshot and corrected my question. Please do have a look – sady Nov 29 '16 at 08:30
  • Possible duplicate of [How to set JSpinner as non editable?](http://stackoverflow.com/questions/2902101/how-to-set-jspinner-as-non-editable) – AxelH Nov 29 '16 at 08:32
  • *"I have updated screenshot and corrected my question."* 'Corrected'? Not with any suggestion I made. Follow the links, **read** the articles. I'll consider retracting the down & close votes after your next edit. – Andrew Thompson Nov 29 '16 at 08:34

1 Answers1

3

You are recreating a spinner on this event but you already have an instance as Datespin that is visible. So you need to use it instead of a new Instance that doesn't have any link with your GUI

So this should look like :

private void hDateSpinnerPropertyChange(java.beans.PropertyChangeEvent evt) {                                            
        JFormattedTextField spin=((JSpinner.DefaultEditor)Datespin.getEditor()).getTextField();
        spin.setEditable(false);
}
AxelH
  • 14,325
  • 2
  • 25
  • 55
  • @sady You are welcome, that happen. Try to debug your code next time, you would have notice that those JSpinner were different ;) – AxelH Nov 30 '16 at 06:23