• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: HideComboArrowButton
title: JComboBoxのArrowButtonを隠す
tags: [JComboBox, ArrowButton, UIManager]
author: aterai
pubdate: 2008-12-22T13:06:25+09:00
description: ArrowButtonを隠して、JComboBoxの表示をJLabel風にします。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTN0Yp0GRI/AAAAAAAAAbY/WvXw5vm2_LI/s800/HideComboArrowButton.png
---
* 概要 [#summary]
`ArrowButton`を隠して、`JComboBox`の表示を`JLabel`風にします。

#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTN0Yp0GRI/AAAAAAAAAbY/WvXw5vm2_LI/s800/HideComboArrowButton.png)

* サンプルコード [#sourcecode]
#code(link){{
JPanel p = new JPanel(new BorderLayout(5, 5));
Object[] items = {"JComboBox 11111:", "JComboBox 222:", "JComboBox 33:"};

UIManager.put("ComboBox.squareButton", Boolean.FALSE);
JComboBox comboBox = new JComboBox(items);
comboBox.setUI(new BasicComboBoxUI() {
  @Override protected JButton createArrowButton() {
    JButton button = new JButton(); //super.createArrowButton();
    button.setBorder(BorderFactory.createEmptyBorder());
    button.setVisible(false);
    return button;
  }
});
comboBox.setOpaque(true);
comboBox.setBackground(p.getBackground());
comboBox.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
comboBox.setFocusable(false);

UIManager.put("ComboBox.squareButton", Boolean.TRUE);
}}

* 解説 [#explanation]
上記のサンプルでは、以下のようにして、`JComboBox`を`JLabel`風に表示しています。
上記のサンプルでは、以下のようにして`JComboBox`の矢印ボタンを非表示に設定しています。

- `UIManager.put("ComboBox.squareButton", Boolean.FALSE);`で、`ArrowButton`の幅をそのまま使用するように変更
- `BasicComboBoxUI#createArrowButton`をオーバーライドして、`ArrowButton`の代わりに幅高さ`0`で`setVisible(false)`なボタンを作成
- `JComboBox`の背景色を親の`JPanel`と同じにする
- `JComboBox`がフォーカスを取得しないようにする
- `UIManager.put("ComboBox.squareButton", Boolean.FALSE)`を設定して`JComboBox`の高さと同じ幅ではなく`ArrowButton`の幅をそのまま使用するように変更
- `BasicComboBoxUI#createArrowButton`をオーバーライドして`ArrowButton`の代わりに幅と高さが`0`で`setVisible(false)`な`JButton`を作成
- `JComboBox`の背景色を親の`JPanel`と同じ色に変更
- `JComboBox`がフォーカスを取得不可になるよう設定

* 参考リンク [#reference]
- [https://community.oracle.com/thread/1359216 Swing - Hide JComboBox Arrow?]
- [http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6337518 Bug ID: 6337518 Null Arrow Button Throws Exception in BasicComboBoxUI]
- [https://bugs.openjdk.org/browse/JDK-6337518 Bug ID: 6337518 Null Arrow Button Throws Exception in BasicComboBoxUI]

* コメント [#comment]
#comment
#comment