• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:LayoutManagerを使ってパネルの展開アニメーションを行う
#navi(../)
RIGHT:Posted by [[terai]] at 2010-11-22
*LayoutManagerを使ってパネルの展開アニメーションを行う [#abd38a62]
パネルの展開・収納をアニメーションで行うLayoutManagerを作成します。

-&jnlp;
-&jar;
-&zip;

#screenshot

**サンプルコード [#n49cf6c2]
#code{{
private javax.swing.Timer animator = null;
private boolean isHidden = true;
private final JPanel controls = new JPanel(new BorderLayout(5, 5) {
  private int controlsHeight = 0;
  private int controlsPreferredHeight = 0;
  @Override public Dimension preferredLayoutSize(Container target) {
    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 javax.swing.Timer(5, new ActionListener() {
        @Override public void actionPerformed(ActionEvent e) {
          controls.revalidate();
        }
      });
      animator.start();
    }
  };
}
}}

**解説 [#s0e07406]
上記のサンプルでは、LayoutManager#preferredLayoutSize(...)をオーバーライドして、パネルの高さを更新するアニメーションを行っています。

//**参考リンク
**コメント [#ff2f0440]
#comment