Example usage for javax.swing SpinnerDateModel SpinnerDateModel

List of usage examples for javax.swing SpinnerDateModel SpinnerDateModel

Introduction

In this page you can find the example usage for javax.swing SpinnerDateModel SpinnerDateModel.

Prototype

public SpinnerDateModel(Date value, Comparable<Date> start, Comparable<Date> end, int calendarField) 

Source Link

Document

Creates a SpinnerDateModel that represents a sequence of dates between start and end.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, 13); // 1pm

    SpinnerDateModel dateModel = new SpinnerDateModel(calendar.getTime(), null, null, Calendar.HOUR_OF_DAY);
    JSpinner spinner = new JSpinner(dateModel);

    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:00 a"));
    //formatter.setFormat(new SimpleDateFormat("HH:00 a"));
}

From source file:MainClass.java

public static void main(String[] args) {
    JFrame frame = new JFrame();

    Calendar now = Calendar.getInstance();
    Calendar earliest = (Calendar) now.clone();
    earliest.add(Calendar.MONTH, -6);
    Calendar latest = (Calendar) now.clone();
    latest.add(Calendar.MONTH, 6);
    SpinnerModel model = new SpinnerDateModel(now.getTime(), earliest.getTime(), latest.getTime(),
            Calendar.WEEK_OF_YEAR);
    final JSpinner spinner = new JSpinner(model);

    model.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println(((SpinnerDateModel) e.getSource()).getDate());
        }//from ww  w .  j a v a 2s.c  o m
    });

    frame.getContentPane().add("North", new JLabel("Choose a week"));
    frame.getContentPane().add("Center", spinner);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:SpinnerDateStartEndSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JSpinner Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Calendar cal = Calendar.getInstance();
    Date now = cal.getTime();//from  w  w  w  . j a v  a 2s . c om
    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 spinner1 = new JSpinner(model);

    JPanel panel1 = new JPanel(new BorderLayout());
    panel1.add(spinner1, BorderLayout.CENTER);
    frame.add(panel1, BorderLayout.SOUTH);

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

From source file:MainClass.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("JSpinner Dates");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Calendar cal = Calendar.getInstance();
    Date now = cal.getTime();//  w  w  w.j  a v a  2s.co m
    cal.add(Calendar.YEAR, -50);
    Date startDate = cal.getTime();
    cal.add(Calendar.YEAR, 100);
    Date endDate = cal.getTime();
    SpinnerModel model2 = new SpinnerDateModel(now, startDate, endDate, Calendar.YEAR);
    JSpinner spinner2 = new JSpinner(model2);
    JLabel label2 = new JLabel("Range");
    JPanel panel2 = new JPanel(new BorderLayout());

    panel2.add(label2, BorderLayout.WEST);
    panel2.add(spinner2, BorderLayout.CENTER);
    frame.add(panel2, BorderLayout.SOUTH);

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

}

From source file:Main.java

/** 
 * Adds to the given <code>JComponent</code> two date spinners 
 * labeled "To" and "From". Used to specify a specific date range. The 
 * default range is -10 years from today's date through today's date. 
 * @param c - <code>JComponent</code> to add the date spinners to
 *//*from ww w  . j a  va 2s . co m*/
public static void addDateRangePanel(JComponent c) {
    Calendar calendar = Calendar.getInstance();
    JSpinner dateSpinner;

    //Set up dates
    Date initDate = calendar.getTime();
    Date latestDate = calendar.getTime();
    calendar.add(Calendar.YEAR, -10);
    Date earliestDate = calendar.getTime();

    //Date Spinners
    SpinnerModel fromModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.DAY_OF_MONTH);
    dateSpinner = addLabeledSpinner(c, "From: ", fromModel, false);
    dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "MM/dd/yyyy"));

    SpinnerModel toModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.DAY_OF_MONTH);
    dateSpinner = addLabeledSpinner(c, "To: ", toModel, true);
    dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "MM/dd/yyyy"));
}

From source file:Main.java

public Main() {
    setSize(200, 100);/*  w  w  w . ja  v a  2  s.  c  o m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));

    add(new JLabel("Expiration Date:"));
    Date today = new Date();
    JSpinner s = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH));
    JSpinner.DateEditor de = new JSpinner.DateEditor(s, "MM/yy");
    s.setEditor(de);
    add(s);

    setVisible(true);
}

From source file:Main.java

public Main() {
    super("Month Spinner");
    setSize(200, 100);/*w ww  .j av a  2s  . c o  m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));

    c.add(new JLabel("Expiration Date:"));
    Date today = new Date();
    JSpinner s = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH));
    JSpinner.DateEditor de = new JSpinner.DateEditor(s, "MM/yy");
    s.setEditor(de);
    c.add(s);

    setVisible(true);
}

From source file:Main.java

public Main() {
    super("Month Spinner");
    setSize(200, 100);// w w w.  j a  v  a 2s .  c o m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));

    c.add(new JLabel("Expiration Date:"));
    Date today = new Date();
    JSpinner s = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH));
    JSpinner.DateEditor de = new JSpinner.DateEditor(s, "MM/yy");
    s.setEditor(de);
    c.add(s);

    setVisible(true);

    System.out.println(s.getNextValue());
}

From source file:Main.java

public Main() {
    super("Month Spinner");
    setSize(200, 100);/*from  ww w  .j a va 2s  . co m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));

    c.add(new JLabel("Expiration Date:"));
    Date today = new Date();
    JSpinner s = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH));
    JSpinner.DateEditor de = new JSpinner.DateEditor(s, "MM/yy");
    s.setEditor(de);
    c.add(s);

    setVisible(true);

    System.out.println(s.getPreviousValue());
}

From source file:Main.java

public Main() {
    super("Month Spinner");
    setSize(200, 100);/*w w  w . j  ava 2s . c om*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));

    c.add(new JLabel("Expiration Date:"));
    Date today = new Date();
    SpinnerDateModel model = new SpinnerDateModel(today, null, null, Calendar.MONTH);
    JSpinner s = new JSpinner();
    s.setModel(model);
    JSpinner.DateEditor de = new JSpinner.DateEditor(s, "MM/yy");
    s.setEditor(de);

    c.add(s);

    setVisible(true);
}