Java Swing How to - Create JSpinner for Date value








Question

We would like to know how to create JSpinner for Date value.

Answer

import java.awt.BorderLayout;
/*from   w w  w .j a  v  a  2s .c o  m*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;

public class SpinnerDateSample {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    SpinnerModel model1 = new SpinnerDateModel();
    JSpinner spinner1 = new JSpinner(model1);

    JLabel label1 = new JLabel("Dates/Date");
    JPanel panel1 = new JPanel(new BorderLayout());
    panel1.add(label1, BorderLayout.WEST);
    panel1.add(spinner1, BorderLayout.CENTER);
    frame.add(panel1, BorderLayout.CENTER);

    frame.setSize(200, 90);
    frame.setVisible(true);
  }
}