Swing/LayoutAnimation のバックアップの現在との差分(No.13)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/LayoutAnimation へ行く。
- 1 (2010-11-22 (月) 14:41:14)
- 2 (2010-12-07 (火) 11:26:36)
- 3 (2010-12-17 (金) 21:18:30)
- 4 (2011-06-28 (火) 17:10:05)
- 5 (2011-09-27 (火) 19:24:41)
- 6 (2012-12-25 (火) 05:07:52)
- 7 (2014-12-05 (金) 02:23:01)
- 8 (2015-03-02 (月) 11:26:13)
- 9 (2016-05-27 (金) 15:29:56)
- 10 (2017-08-24 (木) 12:57:48)
- 11 (2018-09-07 (金) 16:40:36)
- 12 (2019-09-25 (水) 21:25:24)
- 13 (2021-05-11 (火) 01:25:08)
- 14 (2022-08-01 (月) 14:05:52)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: LayoutAnimation title: LayoutManagerを使ってパネルの展開アニメーションを行う tags: [LayoutManager, Animation, BorderLayout, JTree, JPanel] author: aterai pubdate: 2010-11-22T14:41:14+09:00 description: パネルの展開・収納をアニメーションで行うLayoutManagerを作成します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTO_fTHG-I/AAAAAAAAAdQ/9SHzG18aVW0/s800/LayoutAnimation.png --- * 概要 [#summary] パネルの展開・収納をアニメーションで行う`LayoutManager`を作成します。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTO_fTHG-I/AAAAAAAAAdQ/9SHzG18aVW0/s800/LayoutAnimation.png) * サンプルコード [#sourcecode] #code(link){{ private Timer animator; private boolean isHidden = true; private final JPanel controls = new JPanel(new BorderLayout(5, 5) { private int controlsHeight; private int controlsPreferredHeight; @Override public Dimension preferredLayoutSize(Container target) { // synchronized (target.getTreeLock()) { Dimension ps = super.preferredLayoutSize(target); controlsPreferredHeight = ps.height; if (animator != null) { if (isHidden) { if (controls.getHeight() < controlsPreferredHeight) { controlsHeight += 5; } } else { if (controls.getHeight() > 0) { controlsHeight -= 5; } } if (controlsHeight <= 0) { controlsHeight = 0; animator.stop(); } else if (controlsHeight >= controlsPreferredHeight) { controlsHeight = controlsPreferredHeight; animator.stop(); } } ps.height = controlsHeight; return ps; } }); private Action makeShowHideAction() { return new AbstractAction("Show/Hide Search Box") { @Override public void actionPerformed(ActionEvent e) { if (animator != null && animator.isRunning()) { return; } isHidden = controls.getHeight() == 0; animator = new Timer(5, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { controls.revalidate(); } }); animator.start(); } }; } }} * 解説 [#explanation] 上記のサンプルでは、`LayoutManager#preferredLayoutSize(...)`メソッドをオーバーライドして子パネルの高さを更新し展開アニメーションを表現しています。 - `Timer`を使用して`5ms`ごとに子パネルの高さを更新 - `LayoutManager#preferredLayoutSize(...)`メソッドをオーバーライドして子パネルの高さの変更を展開アニメーションとして表現 - 内部の`JTree`の高さを縮小せずに重ねる状態で検索パネルを表示する場合は`BorderLayout`ではなく`OverlayLayout`を[[JTextAreaをキャプションとして画像上にスライドイン>Swing/EaseInOut]]のように使用する方法がある - 内部の`JTree`の高さを縮小せずに重ねる状態で検索パネルを表示する場合は、`BorderLayout`ではなく`OverlayLayout`を[[JTextAreaをキャプションとして画像上にスライドイン>Swing/EaseInOut]]のように使用する方法がある * 参考リンク [#reference] - [[JTreeのノードを検索する>Swing/SearchBox]] -- `JTree`のノードを検索するサンプル - [[JTextAreaをキャプションとして画像上にスライドイン>Swing/EaseInOut]] * コメント [#comment] #comment #comment