Swing/SpinnerLocalDateTimeModel のバックアップ(No.7)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/SpinnerLocalDateTimeModel へ行く。
- 1 (2015-02-02 (月) 00:14:27)
- 2 (2015-04-06 (月) 18:40:08)
- 3 (2015-04-09 (木) 19:07:38)
- 4 (2015-06-02 (火) 18:49:21)
- 5 (2016-05-26 (木) 17:24:42)
- 6 (2016-06-16 (木) 00:59:24)
- 7 (2016-06-24 (金) 16:33:09)
- 8 (2017-01-09 (月) 03:51:00)
- 9 (2017-01-10 (火) 15:38:56)
- 10 (2017-04-04 (火) 14:13:45)
- 11 (2017-04-07 (金) 13:51:51)
- 12 (2017-12-13 (水) 16:02:37)
- 13 (2019-08-29 (木) 15:59:11)
- 14 (2021-04-23 (金) 20:06:05)
- 15 (2022-08-20 (土) 22:15:25)
- 16 (2023-03-16 (木) 15:43:08)
- 17 (2023-03-18 (土) 15:45:43)
- title: JSpinnerでLocalDateTimeを使用する tags: [JSpinner, SpinnerModel, LocalDateTime] author: aterai pubdate: 2015-02-02T00:12:04+09:00 description: DateやCalendarなどを使用するSpinnerDateModelの代わりに、JDK 8で導入されたLocalDateTimeなどを使用するSpinnerModelを作成して、JSpinnerで日付を選択します。
概要
Date
やCalendar
などを使用するSpinnerDateModel
の代わりに、JDK 8
で導入されたLocalDateTime
などを使用するSpinnerModel
を作成して、JSpinner
で日付を選択します。
Screenshot
Advertisement
サンプルコード
class SpinnerLocalDateTimeModel extends AbstractSpinnerModel implements Serializable {
private Comparable<ChronoLocalDateTime<?>> start, end;
private ChronoLocalDateTime<?> value;
private TemporalUnit temporalUnit;
public SpinnerLocalDateTimeModel(
ChronoLocalDateTime<?> value,
Comparable<ChronoLocalDateTime<?>> start,
Comparable<ChronoLocalDateTime<?>> end,
TemporalUnit temporalUnit) {
super();
if (Objects.nonNull(start) && start.compareTo(value) >= 0
|| Objects.nonNull(end) && end.compareTo(value) <= 0) {
throw new IllegalArgumentException("(start <= value <= end) is false");
}
this.value = Optional.ofNullable(value)
.orElseThrow(() -> new IllegalArgumentException("value is null"));
this.start = start;
this.end = end;
this.temporalUnit = temporalUnit;
}
public void setStart(Comparable<ChronoLocalDateTime<?>> start) {
if (Objects.isNull(start) ? Objects.nonNull(this.start)
: !Objects.equals(start, this.start)) {
this.start = start;
fireStateChanged();
}
}
public Comparable<ChronoLocalDateTime<?>> getStart() {
return start;
}
public void setEnd(Comparable<ChronoLocalDateTime<?>> end) {
if (Objects.isNull(end) ? Objects.nonNull(this.end)
: !Objects.equals(end, this.end)) {
this.end = end;
fireStateChanged();
}
}
public Comparable<ChronoLocalDateTime<?>> getEnd() {
return end;
}
public void setTemporalUnit(TemporalUnit temporalUnit) {
if (temporalUnit != this.temporalUnit) {
this.temporalUnit = temporalUnit;
fireStateChanged();
}
}
public TemporalUnit getTemporalUnit() {
return temporalUnit;
}
@Override public Object getNextValue() {
//Calendar cal = Calendar.getInstance();
//cal.setTime(value.getTime());
//cal.add(calendarField, 1);
//Date next = cal.getTime();
ChronoLocalDateTime<?> next = value.plus(1, temporalUnit);
return Objects.isNull(end) || end.compareTo(next) >= 0 ? next : null;
}
@Override public Object getPreviousValue() {
//Calendar cal = Calendar.getInstance();
//cal.setTime(value.getTime());
//cal.add(calendarField, -1);
//Date prev = cal.getTime();
ChronoLocalDateTime<?> prev = value.minus(1, temporalUnit);
return Objects.isNull(start) || start.compareTo(prev) <= 0 ? prev : null;
}
public ChronoLocalDateTime<?> getLocalDateTime() {
return value;
}
@Override public Object getValue() {
return value;
}
@Override public void setValue(Object value) {
if (!(value instanceof ChronoLocalDateTime<?>)) {
throw new IllegalArgumentException("illegal value");
}
if (!value.equals(this.value)) {
this.value = (LocalDateTime) value;
fireStateChanged();
}
}
}
View in GitHub: Java, Kotlin解説
- 上:
SpinnerDateModel
Calendar
などを使用して開始日、終了日を生成し、SpinnerDateModel
を作成して、JSpinner
に設定Calendar cal = Calendar.getInstance(); cal.clear(Calendar.MILLISECOND); cal.clear(Calendar.SECOND); cal.clear(Calendar.MINUTE); cal.set(Calendar.HOUR_OF_DAY, 0); Date date = cal.getTime(); cal.add(Calendar.DATE, -2); Date start = cal.getTime(); cal.add(Calendar.DATE, 9); Date end = cal.getTime(); JSpinner spinner1 = new JSpinner(new SpinnerDateModel( date, start, end, Calendar.DAY_OF_MONTH));
- 参考: JSpinnerで日付を設定
- 下:
SpinnerLocalDateTimeModel
- 内部で、
Date
ではなく、LocalDateTime
を使用するSpinnerLocalDateTimeModel
(AbstractSpinnerModel
を継承)を作成LocalDateTime d = LocalDateTime.now(); LocalDateTime s = d.minus(2, ChronoUnit.DAYS); LocalDateTime e = d.plus(7, ChronoUnit.DAYS); JSpinner spinner2 = new JSpinner(new SpinnerLocalDateTimeModel( d, s, e, ChronoUnit.DAYS));
- スピナエディタも、以下のような
LocalDateTime
を扱うものに変更- 参考: Temporal Spinners « Java Tips Weblog
class LocalDateTimeEditor extends JSpinner.DefaultEditor { private final DateTimeFormatter dateTimeFormatter; private final SpinnerLocalDateTimeModel model; public LocalDateTimeEditor(final JSpinner spinner, String dateFormatPattern) { super(spinner); if (!(spinner.getModel() instanceof SpinnerLocalDateTimeModel)) { throw new IllegalArgumentException("model not a SpinnerLocalDateTimeModel"); } dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormatPattern); model = (SpinnerLocalDateTimeModel) spinner.getModel(); final DefaultFormatter formatter = new LocalDateTimeFormatter(); EventQueue.invokeLater(new Runnable() { @Override public void run() { formatter.setValueClass(LocalDateTime.class); DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter); JFormattedTextField ftf = (JFormattedTextField) getTextField(); try { String maxString = formatter.valueToString(model.getStart()); String minString = formatter.valueToString(model.getEnd()); ftf.setColumns(Math.max(maxString.length(), minString.length())); } catch (ParseException e) { // PENDING: hmuller e.printStackTrace(); } ftf.setHorizontalAlignment(JTextField.LEFT); ftf.setEditable(true); ftf.setFormatterFactory(factory); } }); } public SpinnerLocalDateTimeModel getModel() { return (SpinnerLocalDateTimeModel) getSpinner().getModel(); } class LocalDateTimeFormatter extends InternationalFormatter { public LocalDateTimeFormatter() { super(dateTimeFormatter.toFormat()); } @Override public String valueToString(Object value) throws ParseException { //System.out.println(value.getClass().getName()); if (value instanceof TemporalAccessor) { //return ((LocalDateTime) value).format(dateTimeFormatter); return dateTimeFormatter.format((TemporalAccessor) value); } else { return ""; } } @Override public Object stringToValue(String text) throws ParseException { //System.out.println("stringToValue:" + text); try { //LocalDateTime value = LocalDate.parse(text, dateTimeFormatter).atStartOfDay(); TemporalAccessor ta = dateTimeFormatter.parse(text); ChronoLocalDateTime<?> value = model.getLocalDateTime(); //@see https://tips4java.wordpress.com/2015/04/09/temporal-spinners/ for (ChronoField field : ChronoField.values()) { if (field.isSupportedBy(value) && ta.isSupported(field)) { value = field.adjustInto(value, ta.getLong(field)); } } Comparable<ChronoLocalDateTime<?>> min = model.getStart(); Comparable<ChronoLocalDateTime<?>> max = model.getEnd(); if (Objects.nonNull(min) && min.compareTo(value) > 0 || Objects.nonNull(max) && max.compareTo(value) < 0) { throw new ParseException(text + " is out of range", 0); } return value; } catch (DateTimeParseException e) { ParseException pe = new ParseException(e.getMessage(), e.getErrorIndex()); pe.setStackTrace(e.getStackTrace()); throw pe; } } } }
- 参考: Temporal Spinners « Java Tips Weblog
- 内部で、