---
category: swing
folder: CalendarWithWeekNumbers
title: JListで週番号を表示するカレンダーを作成する
tags: [JList, JScrollPane, Calendar]
author: aterai
pubdate: 2025-10-27T03:22:24+09:00
description: JListを使用して週番号を表示し、JScrollPaneの行ヘッダーとして配置します。
image: https://drive.google.com/uc?id=1o2DCkEDSE31CKsH78xnXfssxqKn4jEfm
---
* Summary [#summary]
`JList`を使用して週番号を表示し、`JScrollPane`の行ヘッダーとして配置します。
#download(https://drive.google.com/uc?id=1o2DCkEDSE31CKsH78xnXfssxqKn4jEfm)
* Source Code Examples [#sourcecode]
#code(link){{
class WeekNumberListModel extends AbstractListModel<Integer> {
private final WeekFields weekFields = WeekFields.of(Locale.getDefault());
private final LocalDate firstDayOfMonth;
protected WeekNumberListModel(LocalDate date) {
super();
firstDayOfMonth = YearMonth.from(date).atDay(1);
}
@Override public int getSize() {
return CalendarViewListModel.ROW_COUNT;
}
@Override public Integer getElementAt(int index) {
return firstDayOfMonth.plusWeeks(index).get(weekFields.weekOfWeekBasedYear());
return firstDayOfMonth
.plusWeeks(index)
.get(weekFields.weekOfWeekBasedYear());
}
}
}}
* Description [#description]
- セルの高さを月カレンダー本体と同じに設定した`JList`を週番号表示用に作成
-- 週番号表示用`JList`のセル背景色は週ヘッダーのセル背景色と同じになるよう設定
- `AbstractListModel<Integer>#getElementAt(int index)`をオーバーライドして週番号を返す`ListModel`を作成
-- `index`行目の週番号(暦週の基準年の何週目か)は月の初日に`index`週を足して取得した`LocalDate`から[https://docs.oracle.com/javase/jp/8/docs/api/java/time/temporal/WeekFields.html#weekOfWeekBasedYear-- WeekFields.of(Locale).weekOfWeekBasedYear()]で取得
- `JScrollPane#setRowHeaderView(...)`で週番号表示用`JList`を月カレンダーの左の行ヘッダーに配置
-- 表示する月の切り替えが発生すると月カレンダーの`ListModel<LocalDate>`更新と同様に週番号表示用`JList`の`ListModel<Integer>`も入れ替えて更新する
* Reference [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/time/temporal/WeekFields.html#weekOfWeekBasedYear-- WeekFields#weekOfWeekBasedYear() (Java Platform SE 8)]
- [[JListで月のカーソルキー移動や、週を跨いた日付を範囲選択が可能なカレンダーを作成する>Swing/CalendarViewList]]
* Comment [#comment]
#comment
#comment