• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JComboBox内にJButtonを二つレイアウトする
TITLE:JComboBox内にJButtonを左右に二つレイアウトする
#navi(../)
RIGHT:Posted by [[terai]] at 2010-09-20
*JComboBox内にJButtonを二つレイアウトする [#a4ce349d]
*JComboBox内にJButtonを左右に二つレイアウトする [#a4ce349d]
JComboBoxが使用するレイアウトを変更して、検索欄風のコンポーネントを作成します。

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

#screenshot

**サンプルコード [#k8b9a8a7]
#code{{
public class BasicSearchBarComboBoxUI extends SearchBarComboBoxUI{
  public static javax.swing.plaf.ComponentUI createUI(JComponent c) {
    return new BasicSearchBarComboBoxUI();
  }
  protected boolean isEditable = true;
  @Override protected void installDefaults() {
    super.installDefaults();
    //comboBox.setEditable(true);
    comboBox.setPreferredSize(new Dimension(0, 20));
    comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
    //comboBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  }
  @Override protected LayoutManager createLayoutManager() {
    return new LayoutManager() {
      @Override public void addLayoutComponent(String name, Component comp) {}
      @Override public void removeLayoutComponent(Component comp) {}
      @Override public Dimension preferredLayoutSize(Container parent) {
        return parent.getPreferredSize();
      }
      @Override public Dimension minimumLayoutSize(Container parent) {
        return parent.getMinimumSize();
      }
      @Override public void layoutContainer(Container parent) {
        if(!(parent instanceof JComboBox)) return;
        JComboBox cb   = (JComboBox)parent;
        int width    = cb.getWidth();
        int height     = cb.getHeight();
        Insets insets  = cb.getInsets();
        int buttonHeight = height - (insets.top + insets.bottom);
        int buttonWidth  = buttonHeight;
        int loupeWidth   = buttonHeight;

        JButton arrowButton = (JButton)cb.getComponent(0);
        if(arrowButton != null) {
          Insets arrowInsets = arrowButton.getInsets();
          buttonWidth = arrowButton.getPreferredSize().width + arrowInsets.left + arrowInsets.right;
          arrowButton.setBounds(insets.left, insets.top, buttonWidth, buttonHeight);
        }
        JButton loupeButton = null;
        for(Component c: cb.getComponents()) {
          if("ComboBox.loupeButton".equals(c.getName())) {
            loupeButton = (JButton)c;
            break;
          }
        }
        //= (JButton)cb.getComponent(3);
        if(loupeButton != null) {
          Insets loupeInsets = loupeButton.getInsets();
          loupeWidth = loupeButton.getPreferredSize().width + loupeInsets.left + loupeInsets.right;
          loupeButton.setBounds(width - (insets.right + loupeWidth), insets.top, loupeWidth, buttonHeight);
        }
        JTextField editor = (JTextField)cb.getEditor().getEditorComponent();
        //JTextField editor = (JTextField)cb.getComponent(1);
        if(editor != null) {
          editor.setBounds(insets.left + buttonWidth, insets.top,
                   width  - (insets.left + insets.right + buttonWidth + loupeWidth),
                   height - (insets.top  + insets.bottom));
        }
      }
    };
  }
//......
}}

**解説 [#g9ab284c]
上記のサンプルでは、JComboBoxが使用するレイアウトを以下のように変更しています。
-元のArrowButtonは、左側に表示
--JComboBox#setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);とした場合のコードを流用
--アイコンも検索エンジンのものと、下向きの三角の二つを表示するように設定
-LoupeButtonとして、新たにJButtonを追加し、右側に配置
-常に編集可能として、JTextFieldを中央に配置

----
ポップアップを表示、選択してもJTextFieldが変更されないように、PopupMenuListenerでsetTextし直すようにしています。
ポップアップを表示、選択してもJTextFieldが変更されないようにしています。
-JComboBox#putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);として、カーソル移動で変更されないように設定
-選択されてもPopupMenuListenerでsetTextし直すように設定
#code{{
protected PopupMenuListener createPopupMenuListener() {
  if(popupMenuListener == null) {
    popupMenuListener = new PopupMenuListener() {
      private String str;
      public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        JComboBox combo = (JComboBox)e.getSource();
        JTextField editor = (JTextField)combo.getEditor().getEditorComponent();
        str = editor.getText();
      }
      public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        JComboBox combo = (JComboBox)e.getSource();
        final JTextField editor = (JTextField)combo.getEditor().getEditorComponent();
        EventQueue.invokeLater(new Runnable() {
          @Override public void run() {
            editor.setText(str);
          }
        });
      }
      public void popupMenuCanceled(PopupMenuEvent e) {}
    };
  }
  return popupMenuListener;
}
}}

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