Swing/SearchBarLayoutComboBox のバックアップ(No.26)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/SearchBarLayoutComboBox へ行く。
- 1 (2010-09-20 (月) 12:16:36)
- 2 (2010-09-24 (金) 20:20:42)
- 3 (2010-11-16 (火) 02:38:36)
- 4 (2011-01-07 (金) 16:29:00)
- 5 (2011-05-25 (水) 17:46:55)
- 6 (2011-06-05 (日) 02:43:11)
- 7 (2012-12-27 (木) 07:44:52)
- 8 (2012-12-28 (金) 13:03:41)
- 9 (2013-05-26 (日) 05:18:05)
- 10 (2013-07-26 (金) 01:44:52)
- 11 (2014-02-18 (火) 15:26:20)
- 12 (2014-12-02 (火) 01:51:27)
- 13 (2015-02-22 (日) 21:04:03)
- 14 (2015-03-28 (土) 15:37:50)
- 15 (2015-05-22 (金) 18:59:56)
- 16 (2017-03-14 (火) 14:34:56)
- 17 (2018-01-11 (木) 17:46:50)
- 18 (2018-02-24 (土) 19:51:30)
- 19 (2019-05-22 (水) 19:34:28)
- 20 (2020-01-08 (水) 20:43:16)
- 21 (2021-07-10 (土) 04:34:22)
- 22 (2021-11-17 (水) 04:17:12)
- 23 (2025-01-03 (金) 08:57:02)
- 24 (2025-01-03 (金) 09:01:23)
- 25 (2025-01-03 (金) 09:02:38)
- 26 (2025-01-03 (金) 09:03:21)
- 27 (2025-01-03 (金) 09:04:02)
- 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:
hreflang:
href: https://java-swing-tips.blogspot.com/2010/09/searchbar-jcombobox.html lang: en
Summary
JComboBox
が使用するレイアウトを変更して、検索欄風のコンポーネントを作成します。
Screenshot

Advertisement
Source Code Examples
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);
}
}
};
}
// ...
View in GitHub: Java, KotlinExplanation
上記のサンプルでは、JComboBox
が使用するLayoutManager
を以下のように変更しています。
ArrowButton
は右側から左側に移動JComboBox#setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
とした場合のコードを流用- 検索サイトのアイコンと下向きの三角の二つを表示するように設定
LoupeButton
として新たにJButton
を追加し、右側に配置- 常に編集可能として
JTextField
を中央に配置 - ポップアップを表示、選択しても
JTextField
が変更されないように設定JComboBox#putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
としてカーソル移動で変更されないように設定- 選択されても
PopupMenuListener
でsetText(...)
し直すように設定
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;
}