---
category: swing
folder: SearchBarLayoutComboBox
title: JComboBox内にJButtonを左右に二つレイアウトする
tags: [JComboBox, JButton, ArrowButton, LayoutManager, JTextField, PopupMenuListener]
author: aterai
pubdate: 2010-09-20T12:16:36+09:00
description: JComboBoxが使用するレイアウトを変更して、検索欄風のコンポーネントを作成します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTSqLxrLoI/AAAAAAAAAjI/M2OZzogy3-Q/s800/SearchBarLayoutComboBox.png
hreflang:
href: https://java-swing-tips.blogspot.com/2010/09/searchbar-jcombobox.html
lang: en
---
* Summary [#summary]
`JComboBox`が使用するレイアウトを変更して、検索欄風のコンポーネントを作成します。
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTSqLxrLoI/AAAAAAAAAjI/M2OZzogy3-Q/s800/SearchBarLayoutComboBox.png)
* Source Code Examples [#sourcecode]
#code(link){{
public class BasicSearchBarComboBoxUI extends SearchBarComboBoxUI {
protected boolean isEditable = true;
public static javax.swing.plaf.ComponentUI createUI(JComponent c) {
return new BasicSearchBarComboBoxUI();
}
@Override protected void installDefaults() {
super.installDefaults();
// comboBox.setEditable(true);
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) {
/* not needed */
}
@Override public void removeLayoutComponent(Component comp) {
/* not needed */
}
@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;
Rectangle r = SwingUtilities.calculateInnerArea(cb, null);
int arrowSize = 0;
JButton arrowButton = (JButton) cb.getComponent(0);
if (Objects.nonNull(arrowButton)) {
Insets arrowInsets = arrowButton.getInsets();
int bw = arrowButton.getPreferredSize().width + arrowInsets.left + arrowInsets.right;
arrowButton.setBounds(r.x, r.y, bw, r.height);
arrowSize = bw;
}
JButton loupeButton = null;
for (Component c : cb.getComponents()) {
if ("ComboBox.loupeButton".equals(c.getName())) {
loupeButton = (JButton) c;
break;
}
}
int loupeSize = 0;
if (Objects.nonNull(loupeButton)) {
loupeSize = r.height;
loupeButton.setBounds(r.x + r.width - loupeSize, r.y, loupeSize, r.height);
}
JTextField editor = (JTextField) cb.getEditor().getEditorComponent();
if (Objects.nonNull(editor)) {
editor.setBounds(r.x + arrowSize, r.y, r.width - arrowSize - loupeSize, r.height);
}
}
};
}
// ...
}}
* Description [#explanation]
* Description [#description]
上記のサンプルでは、`JComboBox`が使用する`LayoutManager`を以下のように変更しています。
- `ArrowButton`は右側から左側に移動
-- `JComboBox#setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);`とした場合のコードを流用
-- 検索サイトのアイコンと下向きの三角の二つを表示するように設定
- `LoupeButton`として新たに`JButton`を追加し、右側に配置
- 常に編集可能として`JTextField`を中央に配置
- ポップアップを表示、選択しても`JTextField`が変更されないように設定
-- `JComboBox#putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);`としてカーソル移動で変更されないように設定
-- 選択されても`PopupMenuListener`で`setText(...)`し直すように設定
#code{{
protected PopupMenuListener createPopupMenuListener() {
if (popupMenuListener == null) {
popupMenuListener = new PopupMenuListener() {
private String str;
@Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
JComboBox combo = (JComboBox) e.getSource();
str = combo.getEditor().getItem().toString();
}
@Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
Object o = listBox.getSelectedValue();
if (o instanceof SearchEngine) {
SearchEngine se = (SearchEngine) o;
arrowButton.setIcon(se.favicon);
}
JComboBox combo = (JComboBox) e.getSource();
combo.getEditor().setItem(str);
}
@Override public void popupMenuCanceled(PopupMenuEvent e) {}
};
}
return popupMenuListener;
}
}}
* Reference [#reference]
- [[JComboBoxのArrowButtonを隠す>Swing/HideComboArrowButton]]
* Comment [#comment]
#comment
- `editor`にフォーカスがある場合、左のボタンをクリックしてもポップアップメニューが開かないバグを修正。 -- &user(aterai); &new{2010-09-24 (金) 20:20:42};
- `ArrowButton`をクリックして、カーソルキーを押すと`NullPointerException`が発生するバグを修正(この修正?で、ポップアップが表示されている状態で、例えばKBD{G}キーを押しても、項目の`google`は選択されなくなった)。 -- &user(aterai); &new{2011-01-07 (金) 16:29:00};
- `LookAndFeel`を切り替えると、`JComboBox`のフォント設定で、`NullPointerException`が発生するのを修正。 -- &user(aterai); &new{2011-05-25 (水) 17:46:55};
#comment