Example usage for javax.swing SpinnerDateModel getDate

List of usage examples for javax.swing SpinnerDateModel getDate

Introduction

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

Prototype

public Date getDate() 

Source Link

Document

Returns the current element in this sequence of Dates.

Usage

From source file:components.SpinnerDemo3.java

public SpinnerDemo3(boolean cycleMonths) {
    super(new SpringLayout());

    String[] labels = { "Month: ", "Year: ", "Another Date: " };
    int numPairs = labels.length;
    calendar = Calendar.getInstance();
    JFormattedTextField ftf = null;

    //Add the first label-spinner pair.
    String[] monthStrings = getMonthStrings(); //get month names
    SpinnerListModel monthModel = null;
    if (cycleMonths) { //use custom model
        monthModel = new CyclingSpinnerListModel(monthStrings);
    } else { //use standard model
        monthModel = new SpinnerListModel(monthStrings);
    }//from   w  ww.j a  va  2  s.co m
    JSpinner spinner = addLabeledSpinner(this, labels[0], monthModel);
    //Tweak the spinner's formatted text field.
    ftf = getTextField(spinner);
    if (ftf != null) {
        ftf.setColumns(8); //specify more width than we need
        ftf.setHorizontalAlignment(JTextField.RIGHT);
    }

    //Add second label-spinner pair.
    int currentYear = calendar.get(Calendar.YEAR);
    SpinnerModel yearModel = new SpinnerNumberModel(currentYear, //initial value
            currentYear - 100, //min
            currentYear + 100, //max
            1); //step
    //If we're cycling, hook this model up to the month model.
    if (monthModel instanceof CyclingSpinnerListModel) {
        ((CyclingSpinnerListModel) monthModel).setLinkedModel(yearModel);
    }
    spinner = addLabeledSpinner(this, labels[1], yearModel);
    //Make the year be formatted without a thousands separator.
    spinner.setEditor(new JSpinner.NumberEditor(spinner, "#"));

    //Add the third label-spinner pair.
    Date initDate = calendar.getTime();
    calendar.add(Calendar.YEAR, -100);
    Date earliestDate = calendar.getTime();
    calendar.add(Calendar.YEAR, 200);
    Date latestDate = calendar.getTime();
    SpinnerDateModel dateModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.YEAR);//ignored for user input
    dateSpinner = spinner = addLabeledSpinner(this, labels[2], dateModel);
    spinner.setEditor(new JSpinner.DateEditor(spinner, "MM/yyyy"));
    //Tweak the spinner's formatted text field.
    ftf = getTextField(spinner);
    if (ftf != null) {
        ftf.setHorizontalAlignment(JTextField.RIGHT);
        ftf.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 3));
    }
    spinner.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    //XXX: No easy way to get to the buttons and change their border.
    setSeasonalColor(dateModel.getDate()); //initialize color

    //Listen for changes on the date spinner.
    dateSpinner.addChangeListener(this);

    //Lay out the panel.
    SpringUtilities.makeCompactGrid(this, numPairs, 2, //rows, cols
            10, 10, //initX, initY
            6, 10); //xPad, yPad
}