Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code you show appears correct. For reference, here is a working example.</p> <p>Addendum: While the <code>JSpinner</code> has focus, the left and right arrow keys move the caret. The up arrow increments and the down arrow decrements the field containing the caret. The change is (effectively) simultaneous in both the spinner and the label.</p> <p>To access the <code>JFormattedTextField</code> of the <a href="http://download.oracle.com/javase/6/docs/api/javax/swing/JSpinner.DateEditor.html" rel="nofollow noreferrer"><code>JSpinner.DateEditor</code></a>, use the parent's <a href="http://download.oracle.com/javase/6/docs/api/javax/swing/JSpinner.DefaultEditor.html#getTextField%28%29" rel="nofollow noreferrer"><code>getTextField()</code></a> method. A suitable caret listener or text input listener may then be used to update the label as desired.</p> <p>Addendum: Update to use <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/text/DefaultFormatter.html#setCommitsOnValidEdit%28boolean%29" rel="nofollow noreferrer"><code>setCommitsOnValidEdit</code></a>, as suggested <a href="https://stackoverflow.com/questions/3949382/jspinner-value-change-events/7587253#7587253">here</a>.</p> <pre><code>import java.awt.EventQueue; import java.awt.GridLayout; import java.util.Calendar; import java.util.Date; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JSpinner.DateEditor; import javax.swing.SpinnerDateModel; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.text.DefaultFormatter; /** * @see https://stackoverflow.com/questions/2010819 * @see https://stackoverflow.com/questions/3949518 */ public class JSpinnerTest extends JPanel { public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame f = new JFrame("JSpinnerTest"); f.add(new JSpinnerTest()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); } }); } public JSpinnerTest() { super(new GridLayout(0, 1)); final JLabel label = new JLabel(); final JSpinner spinner = new JSpinner(); Calendar calendar = Calendar.getInstance(); Date initDate = calendar.getTime(); calendar.add(Calendar.YEAR, -5); Date earliestDate = calendar.getTime(); calendar.add(Calendar.YEAR, 10); Date latestDate = calendar.getTime(); spinner.setModel(new SpinnerDateModel( initDate, earliestDate, latestDate, Calendar.MONTH)); DateEditor editor = new JSpinner.DateEditor(spinner, "MMM yyyy"); spinner.setEditor(editor); JFormattedTextField jtf = editor.getTextField(); DefaultFormatter formatter = (DefaultFormatter) jtf.getFormatter(); formatter.setCommitsOnValidEdit(true); spinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { JSpinner s = (JSpinner) e.getSource(); label.setText(s.getValue().toString()); } }); label.setText(initDate.toString()); this.add(spinner); this.add(label); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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