開発メモ

開発用のメモです。

Java Swing で DatePicker

package jp.mirageworld.apps.apptest;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class DatePicker extends JDialog implements ActionListener {

    public static boolean                  single     = false;

    public static final int                START_YEAR = 1950;

    protected static final Vector<Integer> YEAR_LIST  = new Vector<Integer>();
    static {
        for (int i = START_YEAR; i < Calendar.getInstance().get(Calendar.YEAR) + 10; i++) {
            YEAR_LIST.add(i);
        }
    }
    protected static final Vector<Integer> MONTH_LIST = new Vector<Integer>();
    static {
        for (int i = 1; i <= 12; i++) {
            MONTH_LIST.add(i);
        }
    }

    protected JFrame                       parent;
    protected JTextField                   target;

    protected JComboBox<Integer>           year;
    protected JComboBox<Integer>           month;
    protected JButton[]                    dateButton = new JButton[49];

    public DatePicker(JFrame frame, JTextField target) {
        super(frame);
        if (single) {
            return;
        }
        this.parent = frame;
        DatePicker.single = true;

        this.parent.setEnabled(false);

        this.target = target;
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[] {
                50, 50, 50, 50, 50, 50, 50, 0
        };
        gridBagLayout.rowHeights = new int[] {
                50, 30, 50, 50, 50, 50, 50, 50, 50, 10, 50, 0
        };
        gridBagLayout.columnWeights = new double[] {
                1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE
        };
        gridBagLayout.rowWeights = new double[] {
                1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, Double.MIN_VALUE
        };
        getContentPane().setLayout(gridBagLayout);

        JPanel headerPane = new JPanel();
        GridBagConstraints gbc_headerPane = new GridBagConstraints();
        gbc_headerPane.gridwidth = 7;
        gbc_headerPane.insets = new Insets(0, 0, 5, 0);
        gbc_headerPane.fill = GridBagConstraints.BOTH;
        gbc_headerPane.gridx = 0;
        gbc_headerPane.gridy = 0;
        getContentPane().add(headerPane, gbc_headerPane);
        GridBagLayout gbl_headerPane = new GridBagLayout();
        gbl_headerPane.columnWidths = new int[] {
                100, 30, 100, 30, 0, 0, 0, 0, 0, 0, 0
        };
        gbl_headerPane.rowHeights = new int[] {
                50, 0
        };
        gbl_headerPane.columnWeights = new double[] {
                0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE
        };
        gbl_headerPane.rowWeights = new double[] {
                0.0, Double.MIN_VALUE
        };
        headerPane.setLayout(gbl_headerPane);

        year = new JComboBox<Integer>(YEAR_LIST);
        year.setSelectedIndex(Calendar.getInstance().get(Calendar.YEAR) - START_YEAR);
        GridBagConstraints gbc_year = new GridBagConstraints();
        gbc_year.insets = new Insets(0, 0, 0, 5);
        gbc_year.fill = GridBagConstraints.BOTH;
        gbc_year.gridx = 0;
        gbc_year.gridy = 0;
        headerPane.add(year, gbc_year);

        JLabel label = new JLabel("\u5E74");
        GridBagConstraints gbc_label = new GridBagConstraints();
        gbc_label.insets = new Insets(0, 0, 0, 5);
        gbc_label.gridx = 1;
        gbc_label.gridy = 0;
        headerPane.add(label, gbc_label);

        month = new JComboBox<Integer>(MONTH_LIST);
        month.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
        GridBagConstraints gbc_month = new GridBagConstraints();
        gbc_month.insets = new Insets(0, 0, 0, 5);
        gbc_month.fill = GridBagConstraints.BOTH;
        gbc_month.gridx = 2;
        gbc_month.gridy = 0;
        headerPane.add(month, gbc_month);

        JLabel label_1 = new JLabel("\u6708");
        GridBagConstraints gbc_label_1 = new GridBagConstraints();
        gbc_label_1.insets = new Insets(0, 0, 0, 5);
        gbc_label_1.gridx = 3;
        gbc_label_1.gridy = 0;
        headerPane.add(label_1, gbc_label_1);

        JButton btnPrevYear = new JButton("<<");
        GridBagConstraints gbc_btnPrevYear = new GridBagConstraints();
        gbc_btnPrevYear.insets = new Insets(0, 0, 0, 5);
        gbc_btnPrevYear.gridx = 5;
        gbc_btnPrevYear.gridy = 0;
        btnPrevYear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                year.setSelectedIndex(Math.max(year.getSelectedIndex() - 1, 0));
            }
        });
        headerPane.add(btnPrevYear, gbc_btnPrevYear);

        JButton btnPrevMonth = new JButton("<");
        GridBagConstraints gbc_btnPrevMonth = new GridBagConstraints();
        gbc_btnPrevMonth.insets = new Insets(0, 0, 0, 5);
        gbc_btnPrevMonth.gridx = 6;
        gbc_btnPrevMonth.gridy = 0;
        btnPrevMonth.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (month.getSelectedIndex() == 0) {
                    year.setSelectedIndex(Math.max(year.getSelectedIndex() - 1, 0));
                    month.setSelectedIndex(11);
                } else {
                    month.setSelectedIndex(Math.max(month.getSelectedIndex() - 1, 0));
                }
            }
        });
        headerPane.add(btnPrevMonth, gbc_btnPrevMonth);

        JButton btnToMonth = new JButton("\u5F53\u6708");
        GridBagConstraints gbc_btnToMonth = new GridBagConstraints();
        gbc_btnToMonth.insets = new Insets(0, 0, 0, 5);
        gbc_btnToMonth.gridx = 7;
        gbc_btnToMonth.gridy = 0;
        btnToMonth.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                year.setSelectedIndex(Calendar.getInstance().get(Calendar.YEAR) - START_YEAR);
                month.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
            }
        });
        headerPane.add(btnToMonth, gbc_btnToMonth);

        JButton btnNextMonth = new JButton(">");
        GridBagConstraints gbc_btnNextMonth = new GridBagConstraints();
        gbc_btnNextMonth.insets = new Insets(0, 0, 0, 5);
        gbc_btnNextMonth.gridx = 8;
        gbc_btnNextMonth.gridy = 0;
        btnNextMonth.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if (month.getSelectedIndex() == 11) {
                        month.setSelectedIndex(0);
                        year.setSelectedIndex(year.getSelectedIndex() + 1);
                    } else {
                        month.setSelectedIndex(month.getSelectedIndex() + 1);
                    }
                } catch (IllegalArgumentException exception) {
                    // no process
                }
            }
        });
        headerPane.add(btnNextMonth, gbc_btnNextMonth);

        JButton btnNextYear = new JButton(">>");
        GridBagConstraints gbc_btnNextYear = new GridBagConstraints();
        gbc_btnNextYear.gridx = 9;
        gbc_btnNextYear.gridy = 0;
        btnNextYear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    year.setSelectedIndex(year.getSelectedIndex() + 1);
                } catch (IllegalArgumentException exception) {
                    // no process
                }
            }
        });
        headerPane.add(btnNextYear, gbc_btnNextYear);

        JLabel lblMon = new JLabel("MON");
        GridBagConstraints gbc_lblMon = new GridBagConstraints();
        gbc_lblMon.insets = new Insets(0, 0, 5, 5);
        gbc_lblMon.gridx = 0;
        gbc_lblMon.gridy = 1;
        getContentPane().add(lblMon, gbc_lblMon);

        JLabel lblTue = new JLabel("TUE");
        GridBagConstraints gbc_lblTue = new GridBagConstraints();
        gbc_lblTue.insets = new Insets(0, 0, 5, 5);
        gbc_lblTue.gridx = 1;
        gbc_lblTue.gridy = 1;
        getContentPane().add(lblTue, gbc_lblTue);

        JLabel lblWed = new JLabel("WED");
        GridBagConstraints gbc_lblWed = new GridBagConstraints();
        gbc_lblWed.insets = new Insets(0, 0, 5, 5);
        gbc_lblWed.gridx = 2;
        gbc_lblWed.gridy = 1;
        getContentPane().add(lblWed, gbc_lblWed);

        JLabel lblThu = new JLabel("THU");
        GridBagConstraints gbc_lblThu = new GridBagConstraints();
        gbc_lblThu.insets = new Insets(0, 0, 5, 5);
        gbc_lblThu.gridx = 3;
        gbc_lblThu.gridy = 1;
        getContentPane().add(lblThu, gbc_lblThu);

        JLabel lblFri = new JLabel("FRI");
        GridBagConstraints gbc_lblFri = new GridBagConstraints();
        gbc_lblFri.insets = new Insets(0, 0, 5, 5);
        gbc_lblFri.gridx = 4;
        gbc_lblFri.gridy = 1;
        getContentPane().add(lblFri, gbc_lblFri);

        JLabel lblSat = new JLabel("SAT");
        GridBagConstraints gbc_lblSat = new GridBagConstraints();
        gbc_lblSat.insets = new Insets(0, 0, 5, 5);
        gbc_lblSat.gridx = 5;
        gbc_lblSat.gridy = 1;
        getContentPane().add(lblSat, gbc_lblSat);

        JLabel lblSun = new JLabel("SUN");
        lblSun.setForeground(Color.RED);
        GridBagConstraints gbc_lblSun = new GridBagConstraints();
        gbc_lblSun.insets = new Insets(0, 0, 5, 0);
        gbc_lblSun.gridx = 6;
        gbc_lblSun.gridy = 1;
        getContentPane().add(lblSun, gbc_lblSun);

        JButton dateButton_00 = new JButton("1");
        GridBagConstraints gbc_dateButton_00 = new GridBagConstraints();
        gbc_dateButton_00.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_00.fill = GridBagConstraints.BOTH;
        gbc_dateButton_00.gridx = 0;
        gbc_dateButton_00.gridy = 2;
        getContentPane().add(dateButton_00, gbc_dateButton_00);

        JButton dateButton_01 = new JButton("2");
        GridBagConstraints gbc_dateButton_01 = new GridBagConstraints();
        gbc_dateButton_01.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_01.fill = GridBagConstraints.BOTH;
        gbc_dateButton_01.gridx = 1;
        gbc_dateButton_01.gridy = 2;
        getContentPane().add(dateButton_01, gbc_dateButton_01);

        JButton dateButton_02 = new JButton("3");
        dateButton_02.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        GridBagConstraints gbc_dateButton_02 = new GridBagConstraints();
        gbc_dateButton_02.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_02.fill = GridBagConstraints.BOTH;
        gbc_dateButton_02.gridx = 2;
        gbc_dateButton_02.gridy = 2;
        getContentPane().add(dateButton_02, gbc_dateButton_02);

        JButton dateButton_03 = new JButton("4");
        GridBagConstraints gbc_dateButton_03 = new GridBagConstraints();
        gbc_dateButton_03.fill = GridBagConstraints.BOTH;
        gbc_dateButton_03.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_03.gridx = 3;
        gbc_dateButton_03.gridy = 2;
        getContentPane().add(dateButton_03, gbc_dateButton_03);

        JButton dateButton_04 = new JButton("5");
        GridBagConstraints gbc_dateButton_04 = new GridBagConstraints();
        gbc_dateButton_04.fill = GridBagConstraints.BOTH;
        gbc_dateButton_04.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_04.gridx = 4;
        gbc_dateButton_04.gridy = 2;
        getContentPane().add(dateButton_04, gbc_dateButton_04);

        JButton dateButton_05 = new JButton("6");
        GridBagConstraints gbc_dateButton_05 = new GridBagConstraints();
        gbc_dateButton_05.fill = GridBagConstraints.BOTH;
        gbc_dateButton_05.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_05.gridx = 5;
        gbc_dateButton_05.gridy = 2;
        getContentPane().add(dateButton_05, gbc_dateButton_05);

        JButton dateButton_06 = new JButton("7");
        GridBagConstraints gbc_dateButton_06 = new GridBagConstraints();
        gbc_dateButton_06.insets = new Insets(0, 0, 5, 0);
        gbc_dateButton_06.fill = GridBagConstraints.BOTH;
        gbc_dateButton_06.gridx = 6;
        gbc_dateButton_06.gridy = 2;
        getContentPane().add(dateButton_06, gbc_dateButton_06);

        JButton dateButton_07 = new JButton("8");
        GridBagConstraints gbc_dateButton_07 = new GridBagConstraints();
        gbc_dateButton_07.fill = GridBagConstraints.BOTH;
        gbc_dateButton_07.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_07.gridx = 0;
        gbc_dateButton_07.gridy = 3;
        getContentPane().add(dateButton_07, gbc_dateButton_07);

        JButton dateButton_08 = new JButton("9");
        GridBagConstraints gbc_dateButton_08 = new GridBagConstraints();
        gbc_dateButton_08.fill = GridBagConstraints.BOTH;
        gbc_dateButton_08.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_08.gridx = 1;
        gbc_dateButton_08.gridy = 3;
        getContentPane().add(dateButton_08, gbc_dateButton_08);

        JButton dateButton_09 = new JButton("10");
        GridBagConstraints gbc_dateButton_09 = new GridBagConstraints();
        gbc_dateButton_09.fill = GridBagConstraints.BOTH;
        gbc_dateButton_09.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_09.gridx = 2;
        gbc_dateButton_09.gridy = 3;
        getContentPane().add(dateButton_09, gbc_dateButton_09);

        JButton dateButton_10 = new JButton("11");
        GridBagConstraints gbc_dateButton_10 = new GridBagConstraints();
        gbc_dateButton_10.fill = GridBagConstraints.BOTH;
        gbc_dateButton_10.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_10.gridx = 3;
        gbc_dateButton_10.gridy = 3;
        getContentPane().add(dateButton_10, gbc_dateButton_10);

        JButton dateButton_11 = new JButton("12");
        GridBagConstraints gbc_dateButton_11 = new GridBagConstraints();
        gbc_dateButton_11.fill = GridBagConstraints.BOTH;
        gbc_dateButton_11.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_11.gridx = 4;
        gbc_dateButton_11.gridy = 3;
        getContentPane().add(dateButton_11, gbc_dateButton_11);

        JButton dateButton_12 = new JButton("13");
        GridBagConstraints gbc_dateButton_12 = new GridBagConstraints();
        gbc_dateButton_12.fill = GridBagConstraints.BOTH;
        gbc_dateButton_12.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_12.gridx = 5;
        gbc_dateButton_12.gridy = 3;
        getContentPane().add(dateButton_12, gbc_dateButton_12);

        JButton dateButton_13 = new JButton("14");
        GridBagConstraints gbc_dateButton_13 = new GridBagConstraints();
        gbc_dateButton_13.fill = GridBagConstraints.BOTH;
        gbc_dateButton_13.insets = new Insets(0, 0, 5, 0);
        gbc_dateButton_13.gridx = 6;
        gbc_dateButton_13.gridy = 3;
        getContentPane().add(dateButton_13, gbc_dateButton_13);

        JButton dateButton_14 = new JButton("15");
        GridBagConstraints gbc_dateButton_14 = new GridBagConstraints();
        gbc_dateButton_14.fill = GridBagConstraints.BOTH;
        gbc_dateButton_14.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_14.gridx = 0;
        gbc_dateButton_14.gridy = 4;
        getContentPane().add(dateButton_14, gbc_dateButton_14);

        JButton dateButton_15 = new JButton("16");
        GridBagConstraints gbc_dateButton_15 = new GridBagConstraints();
        gbc_dateButton_15.fill = GridBagConstraints.BOTH;
        gbc_dateButton_15.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_15.gridx = 1;
        gbc_dateButton_15.gridy = 4;
        getContentPane().add(dateButton_15, gbc_dateButton_15);

        JButton dateButton_16 = new JButton("17");
        GridBagConstraints gbc_dateButton_16 = new GridBagConstraints();
        gbc_dateButton_16.fill = GridBagConstraints.BOTH;
        gbc_dateButton_16.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_16.gridx = 2;
        gbc_dateButton_16.gridy = 4;
        getContentPane().add(dateButton_16, gbc_dateButton_16);

        JButton dateButton_17 = new JButton("18");
        GridBagConstraints gbc_dateButton_17 = new GridBagConstraints();
        gbc_dateButton_17.fill = GridBagConstraints.BOTH;
        gbc_dateButton_17.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_17.gridx = 3;
        gbc_dateButton_17.gridy = 4;
        getContentPane().add(dateButton_17, gbc_dateButton_17);

        JButton dateButton_18 = new JButton("19");
        GridBagConstraints gbc_dateButton_18 = new GridBagConstraints();
        gbc_dateButton_18.fill = GridBagConstraints.BOTH;
        gbc_dateButton_18.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_18.gridx = 4;
        gbc_dateButton_18.gridy = 4;
        getContentPane().add(dateButton_18, gbc_dateButton_18);

        JButton dateButton_19 = new JButton("20");
        GridBagConstraints gbc_dateButton_19 = new GridBagConstraints();
        gbc_dateButton_19.fill = GridBagConstraints.BOTH;
        gbc_dateButton_19.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_19.gridx = 5;
        gbc_dateButton_19.gridy = 4;
        getContentPane().add(dateButton_19, gbc_dateButton_19);

        JButton dateButton_20 = new JButton("21");
        GridBagConstraints gbc_dateButton_20 = new GridBagConstraints();
        gbc_dateButton_20.fill = GridBagConstraints.BOTH;
        gbc_dateButton_20.insets = new Insets(0, 0, 5, 0);
        gbc_dateButton_20.gridx = 6;
        gbc_dateButton_20.gridy = 4;
        getContentPane().add(dateButton_20, gbc_dateButton_20);

        JButton dateButton_21 = new JButton("22");
        GridBagConstraints gbc_dateButton_21 = new GridBagConstraints();
        gbc_dateButton_21.fill = GridBagConstraints.BOTH;
        gbc_dateButton_21.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_21.gridx = 0;
        gbc_dateButton_21.gridy = 5;
        getContentPane().add(dateButton_21, gbc_dateButton_21);

        JButton dateButton_22 = new JButton("23");
        GridBagConstraints gbc_dateButton_22 = new GridBagConstraints();
        gbc_dateButton_22.fill = GridBagConstraints.BOTH;
        gbc_dateButton_22.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_22.gridx = 1;
        gbc_dateButton_22.gridy = 5;
        getContentPane().add(dateButton_22, gbc_dateButton_22);

        JButton dateButton_23 = new JButton("24");
        GridBagConstraints gbc_dateButton_23 = new GridBagConstraints();
        gbc_dateButton_23.fill = GridBagConstraints.BOTH;
        gbc_dateButton_23.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_23.gridx = 2;
        gbc_dateButton_23.gridy = 5;
        getContentPane().add(dateButton_23, gbc_dateButton_23);

        JButton dateButton_24 = new JButton("25");
        GridBagConstraints gbc_dateButton_24 = new GridBagConstraints();
        gbc_dateButton_24.fill = GridBagConstraints.BOTH;
        gbc_dateButton_24.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_24.gridx = 3;
        gbc_dateButton_24.gridy = 5;
        getContentPane().add(dateButton_24, gbc_dateButton_24);

        JButton dateButton_25 = new JButton("26");
        GridBagConstraints gbc_dateButton_25 = new GridBagConstraints();
        gbc_dateButton_25.fill = GridBagConstraints.BOTH;
        gbc_dateButton_25.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_25.gridx = 4;
        gbc_dateButton_25.gridy = 5;
        getContentPane().add(dateButton_25, gbc_dateButton_25);

        JButton dateButton_26 = new JButton("27");
        GridBagConstraints gbc_dateButton_26 = new GridBagConstraints();
        gbc_dateButton_26.fill = GridBagConstraints.BOTH;
        gbc_dateButton_26.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_26.gridx = 5;
        gbc_dateButton_26.gridy = 5;
        getContentPane().add(dateButton_26, gbc_dateButton_26);

        JButton dateButton_27 = new JButton("28");
        GridBagConstraints gbc_dateButton_27 = new GridBagConstraints();
        gbc_dateButton_27.fill = GridBagConstraints.BOTH;
        gbc_dateButton_27.insets = new Insets(0, 0, 5, 0);
        gbc_dateButton_27.gridx = 6;
        gbc_dateButton_27.gridy = 5;
        getContentPane().add(dateButton_27, gbc_dateButton_27);

        JButton dateButton_28 = new JButton("29");
        GridBagConstraints gbc_dateButton_28 = new GridBagConstraints();
        gbc_dateButton_28.fill = GridBagConstraints.BOTH;
        gbc_dateButton_28.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_28.gridx = 0;
        gbc_dateButton_28.gridy = 6;
        getContentPane().add(dateButton_28, gbc_dateButton_28);

        JButton dateButton_29 = new JButton("30");
        GridBagConstraints gbc_dateButton_29 = new GridBagConstraints();
        gbc_dateButton_29.fill = GridBagConstraints.BOTH;
        gbc_dateButton_29.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_29.gridx = 1;
        gbc_dateButton_29.gridy = 6;
        getContentPane().add(dateButton_29, gbc_dateButton_29);

        JButton dateButton_30 = new JButton("31");
        GridBagConstraints gbc_dateButton_30 = new GridBagConstraints();
        gbc_dateButton_30.fill = GridBagConstraints.BOTH;
        gbc_dateButton_30.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_30.gridx = 2;
        gbc_dateButton_30.gridy = 6;
        getContentPane().add(dateButton_30, gbc_dateButton_30);

        JButton dateButton_31 = new JButton("32");
        GridBagConstraints gbc_dateButton_31 = new GridBagConstraints();
        gbc_dateButton_31.fill = GridBagConstraints.BOTH;
        gbc_dateButton_31.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_31.gridx = 3;
        gbc_dateButton_31.gridy = 6;
        getContentPane().add(dateButton_31, gbc_dateButton_31);

        JButton dateButton_32 = new JButton("33");
        GridBagConstraints gbc_dateButton_32 = new GridBagConstraints();
        gbc_dateButton_32.fill = GridBagConstraints.BOTH;
        gbc_dateButton_32.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_32.gridx = 4;
        gbc_dateButton_32.gridy = 6;
        getContentPane().add(dateButton_32, gbc_dateButton_32);

        JButton dateButton_33 = new JButton("34");
        GridBagConstraints gbc_dateButton_33 = new GridBagConstraints();
        gbc_dateButton_33.fill = GridBagConstraints.BOTH;
        gbc_dateButton_33.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_33.gridx = 5;
        gbc_dateButton_33.gridy = 6;
        getContentPane().add(dateButton_33, gbc_dateButton_33);

        JButton dateButton_34 = new JButton("35");
        GridBagConstraints gbc_dateButton_34 = new GridBagConstraints();
        gbc_dateButton_34.fill = GridBagConstraints.BOTH;
        gbc_dateButton_34.insets = new Insets(0, 0, 5, 0);
        gbc_dateButton_34.gridx = 6;
        gbc_dateButton_34.gridy = 6;
        getContentPane().add(dateButton_34, gbc_dateButton_34);

        JButton dateButton_35 = new JButton("36");
        GridBagConstraints gbc_dateButton_35 = new GridBagConstraints();
        gbc_dateButton_35.fill = GridBagConstraints.BOTH;
        gbc_dateButton_35.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_35.gridx = 0;
        gbc_dateButton_35.gridy = 7;
        getContentPane().add(dateButton_35, gbc_dateButton_35);

        JButton dateButton_36 = new JButton("37");
        GridBagConstraints gbc_dateButton_36 = new GridBagConstraints();
        gbc_dateButton_36.fill = GridBagConstraints.BOTH;
        gbc_dateButton_36.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_36.gridx = 1;
        gbc_dateButton_36.gridy = 7;
        getContentPane().add(dateButton_36, gbc_dateButton_36);

        JButton dateButton_37 = new JButton("38");
        GridBagConstraints gbc_dateButton_37 = new GridBagConstraints();
        gbc_dateButton_37.fill = GridBagConstraints.BOTH;
        gbc_dateButton_37.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_37.gridx = 2;
        gbc_dateButton_37.gridy = 7;
        getContentPane().add(dateButton_37, gbc_dateButton_37);

        JButton dateButton_38 = new JButton("39");
        GridBagConstraints gbc_dateButton_38 = new GridBagConstraints();
        gbc_dateButton_38.fill = GridBagConstraints.BOTH;
        gbc_dateButton_38.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_38.gridx = 3;
        gbc_dateButton_38.gridy = 7;
        getContentPane().add(dateButton_38, gbc_dateButton_38);

        JButton dateButton_39 = new JButton("40");
        GridBagConstraints gbc_dateButton_39 = new GridBagConstraints();
        gbc_dateButton_39.fill = GridBagConstraints.BOTH;
        gbc_dateButton_39.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_39.gridx = 4;
        gbc_dateButton_39.gridy = 7;
        getContentPane().add(dateButton_39, gbc_dateButton_39);

        JButton dateButton_40 = new JButton("41");
        GridBagConstraints gbc_dateButton_40 = new GridBagConstraints();
        gbc_dateButton_40.fill = GridBagConstraints.BOTH;
        gbc_dateButton_40.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_40.gridx = 5;
        gbc_dateButton_40.gridy = 7;
        getContentPane().add(dateButton_40, gbc_dateButton_40);

        JButton dateButton_41 = new JButton("42");
        GridBagConstraints gbc_dateButton_41 = new GridBagConstraints();
        gbc_dateButton_41.fill = GridBagConstraints.BOTH;
        gbc_dateButton_41.insets = new Insets(0, 0, 5, 0);
        gbc_dateButton_41.gridx = 6;
        gbc_dateButton_41.gridy = 7;
        getContentPane().add(dateButton_41, gbc_dateButton_41);

        JButton dateButton_42 = new JButton("43");
        GridBagConstraints gbc_dateButton_42 = new GridBagConstraints();
        gbc_dateButton_42.fill = GridBagConstraints.BOTH;
        gbc_dateButton_42.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_42.gridx = 0;
        gbc_dateButton_42.gridy = 8;
        getContentPane().add(dateButton_42, gbc_dateButton_42);

        JButton dateButton_43 = new JButton("44");
        GridBagConstraints gbc_dateButton_43 = new GridBagConstraints();
        gbc_dateButton_43.fill = GridBagConstraints.BOTH;
        gbc_dateButton_43.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_43.gridx = 1;
        gbc_dateButton_43.gridy = 8;
        getContentPane().add(dateButton_43, gbc_dateButton_43);

        JButton dateButton_44 = new JButton("45");
        GridBagConstraints gbc_dateButton_44 = new GridBagConstraints();
        gbc_dateButton_44.fill = GridBagConstraints.BOTH;
        gbc_dateButton_44.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_44.gridx = 2;
        gbc_dateButton_44.gridy = 8;
        getContentPane().add(dateButton_44, gbc_dateButton_44);

        JButton dateButton_45 = new JButton("46");
        GridBagConstraints gbc_dateButton_45 = new GridBagConstraints();
        gbc_dateButton_45.fill = GridBagConstraints.BOTH;
        gbc_dateButton_45.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_45.gridx = 3;
        gbc_dateButton_45.gridy = 8;
        getContentPane().add(dateButton_45, gbc_dateButton_45);

        JButton dateButton_46 = new JButton("46");
        GridBagConstraints gbc_dateButton_46 = new GridBagConstraints();
        gbc_dateButton_46.fill = GridBagConstraints.BOTH;
        gbc_dateButton_46.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_46.gridx = 4;
        gbc_dateButton_46.gridy = 8;
        getContentPane().add(dateButton_46, gbc_dateButton_46);

        JButton dateButton_47 = new JButton("47");
        GridBagConstraints gbc_dateButton_47 = new GridBagConstraints();
        gbc_dateButton_47.fill = GridBagConstraints.BOTH;
        gbc_dateButton_47.insets = new Insets(0, 0, 5, 5);
        gbc_dateButton_47.gridx = 5;
        gbc_dateButton_47.gridy = 8;
        getContentPane().add(dateButton_47, gbc_dateButton_47);

        JButton dateButton_48 = new JButton("48");
        GridBagConstraints gbc_dateButton_48 = new GridBagConstraints();
        gbc_dateButton_48.fill = GridBagConstraints.BOTH;
        gbc_dateButton_48.insets = new Insets(0, 0, 5, 0);
        gbc_dateButton_48.gridx = 6;
        gbc_dateButton_48.gridy = 8;
        getContentPane().add(dateButton_48, gbc_dateButton_48);

        JButton btnClose = new JButton("Close");
        GridBagConstraints gbc_btnClose = new GridBagConstraints();
        gbc_btnClose.gridwidth = 2;
        gbc_btnClose.fill = GridBagConstraints.BOTH;
        gbc_btnClose.gridx = 5;
        gbc_btnClose.gridy = 10;
        btnClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DatePicker.this.dispose();
            }
        });
        getContentPane().add(btnClose, gbc_btnClose);

        JButton btnClear = new JButton("Clear");
        GridBagConstraints gbc_btnClear = new GridBagConstraints();
        gbc_btnClear.fill = GridBagConstraints.BOTH;
        gbc_btnClear.gridwidth = 2;
        gbc_btnClear.insets = new Insets(0, 0, 0, 5);
        gbc_btnClear.gridx = 3;
        gbc_btnClear.gridy = 10;
        btnClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DatePicker.this.target.setText("");
                DatePicker.this.dispose();
            }
        });
        getContentPane().add(btnClear, gbc_btnClear);

        int i = 49;
        dateButton[--i] = dateButton_48;
        dateButton[--i] = dateButton_47;
        dateButton[--i] = dateButton_46;
        dateButton[--i] = dateButton_45;
        dateButton[--i] = dateButton_44;
        dateButton[--i] = dateButton_43;
        dateButton[--i] = dateButton_42;
        dateButton[--i] = dateButton_41;
        dateButton[--i] = dateButton_40;
        dateButton[--i] = dateButton_39;
        dateButton[--i] = dateButton_38;
        dateButton[--i] = dateButton_37;
        dateButton[--i] = dateButton_36;
        dateButton[--i] = dateButton_35;
        dateButton[--i] = dateButton_34;
        dateButton[--i] = dateButton_33;
        dateButton[--i] = dateButton_32;
        dateButton[--i] = dateButton_31;
        dateButton[--i] = dateButton_30;
        dateButton[--i] = dateButton_29;
        dateButton[--i] = dateButton_28;
        dateButton[--i] = dateButton_27;
        dateButton[--i] = dateButton_26;
        dateButton[--i] = dateButton_25;
        dateButton[--i] = dateButton_24;
        dateButton[--i] = dateButton_23;
        dateButton[--i] = dateButton_22;
        dateButton[--i] = dateButton_21;
        dateButton[--i] = dateButton_20;
        dateButton[--i] = dateButton_19;
        dateButton[--i] = dateButton_18;
        dateButton[--i] = dateButton_17;
        dateButton[--i] = dateButton_16;
        dateButton[--i] = dateButton_15;
        dateButton[--i] = dateButton_14;
        dateButton[--i] = dateButton_13;
        dateButton[--i] = dateButton_12;
        dateButton[--i] = dateButton_11;
        dateButton[--i] = dateButton_10;
        dateButton[--i] = dateButton_09;
        dateButton[--i] = dateButton_08;
        dateButton[--i] = dateButton_07;
        dateButton[--i] = dateButton_06;
        dateButton[--i] = dateButton_05;
        dateButton[--i] = dateButton_04;
        dateButton[--i] = dateButton_03;
        dateButton[--i] = dateButton_02;
        dateButton[--i] = dateButton_01;
        dateButton[--i] = dateButton_00;

        actionPerformed(null);

        year.addActionListener(this);
        month.addActionListener(this);

        setSize(600, 400);
        setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, year.getSelectedIndex() + START_YEAR);
        c.set(Calendar.MONTH, month.getSelectedIndex());
        c.set(Calendar.DAY_OF_MONTH, 1);
        int yMonth = c.get(Calendar.MONTH);

        while (c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            c.add(Calendar.DATE, -1);
        }
        for (JButton b : dateButton) {
            int xMonth = c.get(Calendar.MONTH);
            b.setEnabled(xMonth == yMonth);
            b.setText(Integer.toString(c.get(Calendar.DAY_OF_MONTH)));
            c.add(Calendar.DATE, 1);

            ActionListener[] listeners = b.getActionListeners();
            for (ActionListener listener : listeners) {
                b.removeActionListener(listener);
            }

            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JButton b = (JButton) e.getSource();

                    int y = Integer.valueOf(year.getSelectedItem().toString());
                    int m = Integer.valueOf(month.getSelectedItem().toString());
                    int d = Integer.valueOf(b.getText());

                    String fmt = String.format("%04d.%02d.%02d", y, m, d);
                    if (target == null) {
                        System.out.println(fmt);
                    } else {
                        target.setText(fmt);
                    }
                    DatePicker.this.dispose();
                }
            });
        }
    }

    @Override
    public void dispose() {
        single = false;
        parent.setEnabled(true);
        if (target != null) {
            target.setBackground(Color.WHITE);
            target.setFocusable(false);
            target.setFocusable(true);
        }
        super.dispose();
    }
}
Twitter: @asahina_alice