Java SpinnerDateModel create

Introduction

We can create the SpinnerDateModel as follows:

SpinnerModel model = new SpinnerDateModel();
JSpinner spinner = new JSpinner(model);

Calendar cal = Calendar.getInstance();
Date now = cal.getTime();/*from  w  w  w.j  a v  a  2 s  .  co  m*/
cal.add(Calendar.YEAR, -50);
Date startDate = cal.getTime();
cal.add(Calendar.YEAR, 100);
Date endDate = cal.getTime();
SpinnerModel model = new SpinnerDateModel(now, startDate, endDate, Calendar.YEAR);
JSpinner spinner = new JSpinner(model);
import java.awt.FlowLayout;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;

public class Main extends JFrame {
   public Main() {
      super("java2s.com");

      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setLayout(new FlowLayout());

      Calendar calendar = Calendar.getInstance();
      calendar.set(1950, 1, 1);/*from  www  .j a va 2  s .  c o  m*/
      Date minValue = calendar.getTime();
      calendar.set(2050, 12, 31);
      Date maxValue = calendar.getTime();
      Date currentValue = new Date();
      int steps = Calendar.DAY_OF_MONTH; // Must be a Calendar field
      SpinnerDateModel dModel = new SpinnerDateModel(currentValue, minValue,
          maxValue, steps);
      JSpinner spinner = new JSpinner(dModel);

      getContentPane().add(spinner);
   }

   public static void main(String[] args) {
      Main frame = new Main();
      frame.pack();
      frame.setVisible(true);
   }
}



PreviousNext

Related