Swing/SpinnerDateModel のバックアップの現在との差分(No.8)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/SpinnerDateModel へ行く。
- 1 (2012-11-23 (金) 04:42:33)
- 2 (2012-12-19 (水) 01:56:47)
- 3 (2013-09-06 (金) 00:00:04)
- 4 (2014-03-13 (木) 12:39:54)
- 5 (2014-11-25 (火) 03:03:31)
- 6 (2014-11-26 (水) 19:11:01)
- 7 (2016-02-16 (火) 19:29:02)
- 8 (2016-04-15 (金) 15:23:03)
- 9 (2017-07-28 (金) 18:23:18)
- 10 (2018-08-01 (水) 21:31:14)
- 11 (2020-08-04 (火) 21:08:33)
- 12 (2021-12-28 (火) 12:16:12)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: SpinnerDateModel title: JSpinnerで日付を設定 tags: [JSpinner, SpinnerDateModel, FocusListener, Calendar] author: aterai pubdate: 2011-08-22T15:46:11+09:00 description: JSpinnerに、下限値を設定したSpinnerDateModelを設定して、日付の変更をテストします。 image: https://lh5.googleusercontent.com/-llHXaOVDQbQ/TlH5yRAODSI/AAAAAAAABBE/XsSUtm7J_U0/s800/SpinnerDateModel.png --- * 概要 [#e79d682c] * 概要 [#summary] `JSpinner`に、下限値を設定した`SpinnerDateModel`を設定して、日付の変更をテストします。 #download(https://lh5.googleusercontent.com/-llHXaOVDQbQ/TlH5yRAODSI/AAAAAAAABBE/XsSUtm7J_U0/s800/SpinnerDateModel.png) * サンプルコード [#b916b77a] * サンプルコード [#sourcecode] #code(link){{ final String dateFormatPattern = "yyyy/MM/dd"; String dateFormat= "yyyy/MM/dd"; JSpinner s = new JSpinner( new SpinnerDateModel(date, start, null, Calendar.DAY_OF_MONTH)); final JSpinner.DateEditor editor = new JSpinner.DateEditor(s, dateFormatPattern); JSpinner.DateEditor editor = new JSpinner.DateEditor(s, dateFormat); s.setEditor(editor); editor.getTextField().addFocusListener(new FocusAdapter() { @Override public void focusGained(FocusEvent e) { EventQueue.invokeLater(new Runnable() { @Override public void run() { int i = dateFormatPattern.lastIndexOf("dd"); editor.getTextField().select(i, i + 2); } EventQueue.invokeLater(() -> { int i = dateFormat.lastIndexOf("dd"); editor.getTextField().select(i, i + 2); }); } }); }} * 解説 [#a7924332] * 解説 [#explanation] - `Calendar.DAY_OF_MONTH` -- 本日を現在の値と下限値、上限値は`null`(制限無し)、増減値を`Calendar.DAY_OF_MONTH`とした`SpinnerDateModel`を使用 --- 下限値が現在時刻(例: `Mon Aug 22 15:09:27 JST 2011`)なので、現在の値(`Mon Aug 22 00:00:00 JST 2011`)が範囲外となり、矢印ボタンで日付を変更できない --- 下限値が現在時刻(例: `Mon Aug 22 15:09:27 JST 2011`)なので現在の値(`Mon Aug 22 00:00:00 JST 2011`)が範囲外となり矢印ボタンで日付を変更できない --- 参考: [https://community.oracle.com/thread/2268752 Swing - DateSpinner spins only after an edit] - `min: set(Calendar.HOUR_OF_DAY, 0)` -- 下限値を以下のように本日の始めにリセット --- 例: `Mon Aug 22 15:09:27 JST 2011`を`Mon Aug 22 00:00:00 JST 2011` #code{{ Calendar today = Calendar.getInstance(); today.clear(Calendar.MILLISECOND); today.clear(Calendar.SECOND); today.clear(Calendar.MINUTE); today.set(Calendar.HOUR_OF_DAY, 0); Date start = today.getTime(); System.out.println(date); System.out.println(start); JSpinner spinner2 = new JSpinner(new SpinnerDateModel( date, start, null, Calendar.DAY_OF_MONTH)); spinner2.setEditor(new JSpinner.DateEditor(spinner2, dateFormatPattern)); }} - `JSpinner.DateEditor + FocusListener` -- フォーカスがエディタに移動した場合、日付部分が選択状態になるようリスナーを設定 -- フォーカスがエディタに移動した場合、日付部分が選択状態になるよう`FocusListener`を設定 --- 矢印ボタンのクリックで編集開始した場合、先頭の年度部分ではなく日付が増減する --- 参考: [[CellEditorをJSpinnerにして日付を変更>Swing/DateCellEditor]] * 参考リンク [#r0bd2cc7] * 参考リンク [#reference] - [https://community.oracle.com/thread/2268752 Swing - DateSpinner spins only after an edit] - [[CellEditorをJSpinnerにして日付を変更>Swing/DateCellEditor]] - [[JSpinnerでLocalDateTimeを使用する>Swing/SpinnerLocalDateTimeModel]] -- `Date`ではなく、`LocalDateTime`を使用する方法 * コメント [#h4700b7a] * コメント [#comment] #comment #comment