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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

class MeApp {
    public static void main(String[] args) {
        final int year;
        if (args.length == 0) {
            year = Calendar.getInstance().get(Calendar.YEAR);
        } else {
            year = Integer.valueOf(args[0]);
        }
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                final MeFrame frame = new MeFrame(year);
                frame.setDefaultCloseOperation(MeFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

class MeFrame extends JFrame {
    public MeFrame(int year) {
        super(String.valueOf(year));
        getContentPane().setLayout(new GridLayout(3, 4, 5, 5));
        for (int i = 0; i < 12; ++i) {
            getContentPane().add(new MeMonthPanel(year, i + 1));
        }
    }
}

class MeMonthPanel extends JPanel {
    public MeMonthPanel(int year, int month) {
        setLayout(new BorderLayout());
        add(new JLabel(new DateFormatSymbols().getMonths()[month - 1], JLabel.CENTER), BorderLayout.NORTH);
        add(new JScrollPane(new JTable(new MeMonthTableModel(year, month))), BorderLayout.CENTER);
    }
    public Dimension getPreferredSize() {
        return new Dimension(220, 180); // ???
    }
}

class MeMonthTableModel extends AbstractTableModel {
    private final int _year, _month;
    private final String[] _weekdays = new DateFormatSymbols().getShortWeekdays();
    private final Calendar _cal = Calendar.getInstance();

    public MeMonthTableModel(int year, int month) {
        _year = year; _month = month;
    }
    public int getColumnCount() {
        return 7;
    }
    public String getColumnName(int col) {
        return _weekdays[col + 1];
    }
    public int getRowCount() {
        return 6;
    }
    public Object getValueAt(int row, int col) {
        _cal.set(Calendar.YEAR, _year);
        _cal.set(Calendar.MONTH, _month - 1);      // starts from 0
        _cal.set(Calendar.DAY_OF_WEEK, col + 1);   // starts from 1
        _cal.set(Calendar.WEEK_OF_MONTH, row + 1); // starts from 1
        if (_cal.get(Calendar.DAY_OF_WEEK) == col + 1 && _cal.get(Calendar.WEEK_OF_MONTH) == row + 1) {
            return String.valueOf(_cal.get(Calendar.DATE));
        } else {
            return "";
        }
    }
}