Code
public final LocalDate realLocalDate = LocalDate.now();
public LocalDate currentLocalDate;
public final Dimension size = new Dimension(40, 26);
//...
JLabel yearMonthLabel = new JLabel("", SwingConstants.CENTER);
JList< LocalDate> monthList = new JList< LocalDate>(new CalendarViewListModel(realLocalDate)) {
@Override public void updateUI() {
setCellRenderer(null);
super.updateUI();
setLayoutOrientation(JList.HORIZONTAL_WRAP);
setVisibleRowCount(CalendarViewListModel.ROW_COUNT); // ensure 6 rows in the list
setFixedCellWidth(size.width);
setFixedCellHeight(size.height);
setCellRenderer(new CalendarListRenderer<>());
getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
}
};
monthList.getActionMap().put("selectNextIndex", new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
int index = monthList.getLeadSelectionIndex();
if (index < monthList.getModel().getSize() - 1) {
monthList.setSelectedIndex(index + 1);
} else {
LocalDate d = monthList.getModel()
.getElementAt(monthList.getModel().getSize() - 1)
.plusDays(1);
updateMonthView(currentLocalDate.plusMonths(1));
monthList.setSelectedValue(d, false);
}
}
});
//...
class CalendarViewListModel extends AbstractListModel< LocalDate> {
public static final int ROW_COUNT = 6;
private final LocalDate startDate;
private final WeekFields weekFields = WeekFields.of(Locale.getDefault());
protected CalendarViewListModel(LocalDate date) {
super();
LocalDate firstDayOfMonth = YearMonth.from(date).atDay(1);
int dowv = firstDayOfMonth.get(weekFields.dayOfWeek()) - 1;
startDate = firstDayOfMonth.minusDays(dowv);
}
@Override public int getSize() {
return DayOfWeek.values().length * ROW_COUNT;
}
@Override public LocalDate getElementAt(int index) {
return startDate.plusDays(index);
}
}
References
No comments:
Post a Comment