Note that there are some explanatory texts on larger screens.

plurals
  1. POJSpinner giving old values
    text
    copied!<p>I am using couple of JSpinners in my project who display the hour and minutes. When the JSpinner is incremented or decremented i have to save the value to the database. But the problem is that the JSpinners are giving me old values. eg- If the time displayed is <code>09:30</code> and i increment the time to <code>10:30</code>, I am getting 09:30 as the returned value. I am using following code</p> <p>UPDATED SSCCE</p> <pre><code>package spinnerupdation; import java.awt.Container; import java.awt.FlowLayout; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerDateModel; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.text.DateFormatter; import javax.swing.text.DefaultFormatterFactory; /** * * @author Rohan Kandwal */ public class SpinnerUpdation extends JFrame{ public JSpinner spinner; SpinnerUpdation(){ Container pane=this.getContentPane(); JPanel panel=new JPanel(); panel.setLayout(new FlowLayout()); SpinnerDateModel model=new SpinnerDateModel(); model.setCalendarField(Calendar.HOUR); spinner=new JSpinner(); spinner.setModel(model); spinner.setEditor(new JSpinner.DateEditor(spinner,"hh:mm")); panel.add(spinner); pane.add(panel); spinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField(); DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory(); DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter(); // Change the date format to only show the hours formatter.setFormat(new SimpleDateFormat("hh:mm")); //formatter.setCommitsOnValidEdit(true); System.out.println(spinner.getValue()); //System.out.println(tf.getText()); } }); } public static void main(String[] args) { SpinnerUpdation ss=new SpinnerUpdation(); ss.setDefaultCloseOperation(ss.EXIT_ON_CLOSE); ss.setSize(574, 445); //ss.pack(); ss.setLocationRelativeTo(null); ss.setResizable(false); ss.setVisible(true); } } </code></pre> <p>if i am using <code>tf.getText()</code> i am getting the old value twice but if i am using <code>spinner.getValue</code> I am getting the new value but it is in long format</p> <pre><code>Thu Jan 01 10:18:00 IST 1970 Thu Jan 01 11:18:00 IST 1970 </code></pre> <p>How should i format spinner to give only <code>11:18</code> ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload