Java tutorial
//package com.java2s; import java.awt.Container; import java.util.Calendar; import java.util.Date; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JSpinner; import javax.swing.SpinnerDateModel; import javax.swing.SpinnerModel; public class Main { /** * 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 */ 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")); } /** * Creates a <code>JSpinner</code> with the specified label text from the given * <code>SpinnerModel</code> and adds the <code>JSpinner</code> to the given * <code>Container</code>. * @param c - the container to add the spinner to * @param label - the text which to add to the spinner * @param model - the <code>SpinnerModel</code> to make the spinner from * @return a JSpinner created from the <code>SpinnerModel</code> with the * specified label text */ public static JSpinner addLabeledSpinner(Container c, String label, SpinnerModel model) { JLabel l = new JLabel(label); c.add(l); JSpinner spinner = new JSpinner(model); l.setLabelFor(spinner); c.add(spinner); return spinner; } /** * Creates a <code>JSpinner</code> with the specified label text from the given * <code>SpinnerModel</code> and adds the <code>JSpinner</code> to the given * <code>Container</code>. If the wrap parameter is set to true, MigLayout's * "wrap" attribute will be applied to the <code>JTextField</code>, meaning * the next component added will appear on the next line. * (Exception: Will not work if MigLayout's flowy layout constraint is applied, * but it is rarely used MigLayout feature and thus not a common concern; however * if you have set this layout constraint on your <code>JComponent</code> do not * attempt to use the wrap option of this method.) * @param c - the container to add the spinner to * @param label - the text which to add to the spinner * @param model - the <code>SpinnerModel</code> to make the spinner from * @param wrap - indicates whether the MigLayout "wrap" attribute should be * present when this <code>JSpinner</code> is added to the container; if * component does not have the MigLayout as it's layout manager then this * property has no effect * @return a JSpinner created from the <code>SpinnerModel</code> with the * specified label text */ public static JSpinner addLabeledSpinner(Container c, String label, SpinnerModel model, boolean wrap) { JLabel l = new JLabel(label); c.add(l, "align left"); JSpinner spinner = new JSpinner(model); l.setLabelFor(spinner); if (wrap) { c.add(spinner, "align left, wrap"); } else { c.add(spinner, "align left, split"); } return spinner; } }