Swing/SearchBarLayoutComboBox のバックアップの現在との差分(No.15)
-
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: http://java-swing-tips.blogspot.com/2010/09/searchbar-jcombobox.html href: https://java-swing-tips.blogspot.com/2010/09/searchbar-jcombobox.html lang: en
概要
概要
JComboBox
が使用するレイアウトを変更して、検索欄風のコンポーネントを作成します。
Screenshot
Advertisement
サンプルコード
サンプルコード
public class BasicSearchBarComboBoxUI extends SearchBarComboBoxUI {
protected boolean isEditable = true;
public static javax.swing.plaf.ComponentUI createUI(JComponent c) {
return new BasicSearchBarComboBoxUI();
}
protected boolean isEditable = true;
#spanadd
#spanend
@Override protected void installDefaults() {
super.installDefaults();
//comboBox.setEditable(true);
// comboBox.setEditable(true);
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
//comboBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
// comboBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
#spanadd
#spanend
@Override protected LayoutManager createLayoutManager() {
return new LayoutManager() {
@Override public void addLayoutComponent(String name, Component comp) {}
@Override public void removeLayoutComponent(Component comp) {}
@Override public void addLayoutComponent(String name, Component comp) {
/* not needed */
}
#spanadd
#spanend
@Override public void removeLayoutComponent(Component comp) {
/* not needed */
}
#spanadd
#spanend
@Override public Dimension preferredLayoutSize(Container parent) {
return parent.getPreferredSize();
}
#spanadd
#spanend
@Override public Dimension minimumLayoutSize(Container parent) {
return parent.getMinimumSize();
}
#spanadd
#spanend
@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;
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 (arrowButton != null) {
if (Objects.nonNull(arrowButton)) {
Insets arrowInsets = arrowButton.getInsets();
buttonWidth = arrowButton.getPreferredSize().width +
arrowInsets.left + arrowInsets.right;
arrowButton.setBounds(insets.left, insets.top, buttonWidth, buttonHeight);
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()) {
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);
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();
//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));
if (Objects.nonNull(editor)) {
editor.setBounds(r.x + arrowSize, r.y, r.width - arrowSize - loupeSize, r.height);
}
}
};
}
#spandel
//......
#spanend
// ...
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JComboBox
が使用するレイアウトを以下のように変更しています。
解説
上記のサンプルでは、JComboBox
が使用するLayoutManager
を以下のように変更しています。
- 元の
ArrowButton
は、左側に表示 -
ArrowButton
は右側から左側に移動JComboBox#setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
とした場合のコードを流用- アイコンも検索エンジンのものと、下向きの三角の二つを表示するように設定
-
LoupeButton
として、新たにJButton
を追加し、右側に配置 - 常に編集可能として、
JTextField
を中央に配置- 検索サイトのアイコンと下向きの三角の二つを表示するように設定
-
LoupeButton
として新たにJButton
を追加し、右側に配置 - 常に編集可能として
JTextField
を中央に配置 - ポップアップを表示、選択しても
JTextField
が変更されないように設定-
JComboBox#putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
としてカーソル移動で変更されないように設定 - 選択されても
PopupMenuListener
でsetText(...)
し直すように設定
-
- -
ポップアップを表示、選択しても
JTextField
が変更されないようにしています。
-
JComboBox#putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
として、カーソル移動で変更されないように設定 - 選択されても
PopupMenuListener
でsetText
し直すように設定
protected PopupMenuListener createPopupMenuListener() {
if (popupMenuListener == null) {
popupMenuListener = new PopupMenuListener() {
private String str;
#spanadd
#spanend
@Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
JComboBox combo = (JComboBox) e.getSource();
str = combo.getEditor().getItem().toString();
}
#spanadd
#spanend
@Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
Object o = listBox.getSelectedValue();
if (o instanceof SearchEngine) {
SearchEngine se = (SearchEngine) o;
arrowButton.setIcon(se.favicon);
}
final JComboBox combo = (JComboBox) e.getSource();
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
combo.getEditor().setItem(str);
}
});
JComboBox combo = (JComboBox) e.getSource();
combo.getEditor().setItem(str);
}
#spanadd
#spanend
@Override public void popupMenuCanceled(PopupMenuEvent e) {}
};
}
return popupMenuListener;
}