summaryrefslogtreecommitdiff
path: root/sources/client/helper/DateSpinner.java
blob: ed5ada1376e91215355c45eab6a1bba95474e706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* NetCalendar 2006, 2009 (c) Dipl.-Inform. (FH) Paul C. Buetow
 * http://netcalendar.buetow.org - netcalendar@dev.buetow.org
 */

package client.helper;


import java.awt.FlowLayout;
import java.util.Calendar;
import java.util.Date;

import javax.swing.*;

/**
 * This helper class helps to create a date spinner to edit Date objects.
 * @author Paul C. Buetow
 */
public class DateSpinner extends JComponent {
    private final static long serialVersionUID = 1L;
    private Date date;
    private SpinnerDateModel spinnerDateModel;

    /**
     * Creates a date spinner to set/edit a given date. This constructor uses the
     * current date!
     */
    public DateSpinner() {
        this.date = new Date();
        initComponents();
    }

    /**
     * Creates a date spinner to set/edit a given date.
     * @param date The date to be used for the date spinner!
     */
    public DateSpinner(Date date) {
        this.date = date;
        initComponents();
    }

    /**
     * Initializes all the date spinner GUI components.
     */
    private void initComponents() {
        setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));

        spinnerDateModel = new SpinnerDateModel(date, null, null, Calendar.MONTH);
        JSpinner jSpinner = new JSpinner(spinnerDateModel);
        new JSpinner.DateEditor(jSpinner, "MM/yy");

        add(jSpinner);
    }

    /**
     * Returns the date which is represented by this spinner.
     * @return Returns the date which is represented by this spinner.
     */
    public Date getDate() {
        return spinnerDateModel.getDate();
    }
}