Swing/SmoothScroll のバックアップソース(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/SmoothScroll へ行く。
- 1 (2007-08-13 (月) 13:01:23)
- 2 (2011-06-01 (水) 03:09:21)
- 3 (2013-02-01 (金) 11:28:46)
- 4 (2014-12-24 (水) 14:51:07)
- 5 (2016-03-27 (日) 19:51:07)
- 6 (2017-07-26 (水) 16:43:19)
- 7 (2018-07-27 (金) 16:27:58)
- 8 (2020-07-30 (木) 11:58:28)
- 9 (2021-12-21 (火) 16:30:51)
- 10 (2025-01-03 (金) 08:57:02)
- 11 (2025-01-03 (金) 09:01:23)
- 12 (2025-01-03 (金) 09:02:38)
- 13 (2025-01-03 (金) 09:03:21)
- 14 (2025-01-03 (金) 09:04:02)
- 15 (2025-06-19 (木) 12:41:37)
- 16 (2025-06-19 (木) 12:43:47)
--- title: JTextAreaでSmoothScrollによる行移動 tags: [JScrollPane, JViewport, Animation, JTextArea] author: aterai pubdate: 2007-08-13T13:01:23+09:00 description: SmoothScrollアニメーション有りで任意の行まで移動します。 --- * 概要 [#qdf0c9e9] `SmoothScroll`アニメーション有りで任意の行まで移動します。 #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTTSaxFSzI/AAAAAAAAAkI/KtedLqwCXBY/s800/SmoothScroll.png) * サンプルコード [#u2a76de6] #code(link){{ Document doc = textArea.getDocument(); Element root = doc.getDefaultRootElement(); int ln = getDestLineNumber(textField, root); if(ln<0) { Toolkit.getDefaultToolkit().beep(); return; } try{ final Element elem = root.getElement(ln-1); final Rectangle dest = textArea.modelToView(elem.getStartOffset()); final Rectangle current = scroll.getViewport().getViewRect(); new Timer(20, new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { Timer animator = (Timer)ae.getSource(); if(dest.y < current.y && animator.isRunning()) { int d = Math.max(1, (current.y-dest.y)/2); current.y = current.y - d; textArea.scrollRectToVisible(current); }else if(dest.y > current.y && animator.isRunning()) { int d = Math.max(1, (dest.y-current.y)/2); current.y = current.y + d; textArea.scrollRectToVisible(current); }else{ textArea.setCaretPosition(elem.getStartOffset()); animator.stop(); } } }).start(); }catch(BadLocationException ble) { Toolkit.getDefaultToolkit().beep(); } }} * 解説 [#kad57b23] `java.swing.Timer`でイベントを発生させ、目的位置と現在位置の差の半分だけ`ViewRect`のスクロールを繰り返すことで、アニメーションを行っています。 * 参考リンク [#ua27efde] - [[JTextAreaの任意の行に移動>Swing/GotoLine]] * コメント [#o3d598ba] #comment #comment