Java JSpinner create from SpinnerDateModel

Description

Java JSpinner create from SpinnerDateModel

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   w w w . j a  va2  s.com
      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