---
category: swing
folder: RightPopupMenuComboBox
title: JComboBoxのPopupMenuを右側に表示する
tags: [JComboBox, JPopupMenu, PopupMenuListener, ArrowButton, Icon]
author: aterai
pubdate: 2010-03-22T02:10:46+09:00
description: JComboBoxの右側にPopupMenuが表示されるように設定します。
image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTR6-BHykI/AAAAAAAAAh8/0mx4AWajd58/s800/RightPopupMenuComboBox.png
---
* 概要 [#summary]
`JComboBox`の右側に`PopupMenu`が表示されるように設定します。

#download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTR6-BHykI/AAAAAAAAAh8/0mx4AWajd58/s800/RightPopupMenuComboBox.png)

* サンプルコード [#sourcecode]
#code(link){{
class RightPopupMenuListener implements PopupMenuListener {
  @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
    EventQueue.invokeLater(() -> {
      JComboBox<?> combo = (JComboBox<?>) e.getSource();
      Accessible a = combo.getAccessibleContext().getAccessibleChild(0);
      // Or Accessible a = combo.getUI().getAccessibleChild(combo, 0);
      if (a instanceof JPopupMenu) {
        JPopupMenu pop = (JPopupMenu) a;
        Point p = new Point(combo.getSize().width, 0);
        SwingUtilities.convertPointToScreen(p, combo);
        pop.setLocation(p);
      }
    });
  }

  @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
    /* not needed */
  }

  @Override public void popupMenuCanceled(PopupMenuEvent e) {
    /* not needed */
  }
}
}}

* 解説 [#explanation]
- `JComboBox`のドロップダウンリストとして`PopupMenu`が開くときにその表示位置を変更する`PopupMenuListener`を作成
-- `JComboBox#addPopupMenuListener(...)`メソッドで追加
- `JComboBox`の矢印アイコンも以下のように変更
#code{{
combo2.setUI(new WindowsComboBoxUI() {
  @Override protected JButton createArrowButton() {
    JButton button = new JButton(icon) {
      @Override public Dimension getPreferredSize() {
        return new Dimension(14, 14);
      }
    };
    button.setRolloverIcon(makeRolloverIcon(icon));
    button.setFocusPainted(false);
    button.setContentAreaFilled(false);
    return button;
  }
});
}}

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/event/PopupMenuListener.html PopupMenuListener (Java Platform SE 8)]

* コメント [#comment]
#comment
- [https://bugs.openjdk.java.net/browse/JDK-4743225 Bug ID: 4743225 Size of JComboBox list is wrong when list is populated via PopupMenuListener]のせいで?、正常に動作しなくなっていたので修正。 -- &user(aterai); &new{2012-04-24 (火) 16:54:17};
- [https://bugs.openjdk.org/browse/JDK-4743225 Bug ID: 4743225 Size of JComboBox list is wrong when list is populated via PopupMenuListener]のせいで?、正常に動作しなくなっていたので修正。 -- &user(aterai); &new{2012-04-24 (火) 16:54:17};

#comment