• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JToggleButtonからポップアップメニューを開く
#navi(../)
*JToggleButtonからポップアップメニューを開く [#e15ce510]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-07-10~
更新日:&lastmod;
Posted by [[terai]] at 2006-07-10

#contents

**概要 [#sd52e2d8]
クリックするとポップアップメニューを表示するJToggleButtonを作成し、これをツールバーに追加します。

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

#screenshot

**サンプルコード [#o01c8911]
#code{{
 class MenuToggleButton extends JToggleButton {
   private final Icon i = new MenuArrowIcon();
   public MenuToggleButton(final AbstractAction aa) {
     super(aa);
     this.setFocusPainted(false);
     this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4+i.getIconWidth()));
   }
   public void paintComponent(Graphics g) {
     super.paintComponent(g);
     Dimension dim = getSize();
     Insets ins = getInsets();
     int x = dim.width-ins.right;
     int y = ins.top+(dim.height-ins.top-ins.bottom-i.getIconHeight())/2;
     i.paintIcon(this, g, x, y);
   }
 }
class MenuToggleButton extends JToggleButton {
  private final Icon i = new MenuArrowIcon();
  public MenuToggleButton(final AbstractAction aa) {
    super(aa);
    this.setFocusPainted(false);
    this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4+i.getIconWidth()));
  }
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Dimension dim = getSize();
    Insets ins = getInsets();
    int x = dim.width-ins.right;
    int y = ins.top+(dim.height-ins.top-ins.bottom-i.getIconHeight())/2;
    i.paintIcon(this, g, x, y);
  }
}
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#i6f6caf9]
上記のサンプルでは、JToggleButtonの右側に余白を設定して、そこに下向きの矢印を上書きしています。

**参考リンク [#f95e2567]
-[[XP Style Icons - Windows Application Icon, Software XP Icons>http://www.icongalore.com/]]
--アイコンを利用しています。
-[[Swing bug? cannot set width of JToggleButton>http://forum.java.sun.com/thread.jspa?threadID=791450]]
--アイコン(矢印ではない)を利用しています。
-[[Swing - Swing bug? cannot set width of JToggleButton>http://forums.sun.com/thread.jspa?threadID=791450]]

**コメント [#h4a49a4d]
#comment