TITLE:JComboBoxのArrowButtonを隠す
#navi(../)
RIGHT:Posted by [[terai]] at 2008-12-22
*JComboBoxのArrowButtonを隠す [#x5fb6947]
ArrowButtonを隠して、JComboBoxの表示をJLabel風にします。

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

#screenshot

**サンプルコード [#l1b78e9d]
#code{{
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() {
  protected JButton createArrowButton() {
    JButton button = new JButton(); //.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);
}}

**解説 [#q1e91fd3]
上記のサンプルでは、以下のようにして、JComboBoxをJLabel風に表示しています。
- UIManager.put("ComboBox.squareButton", Boolean.FALSE); で、ArrowButtonの幅をそのまま使用するように変更
- BasicComboBoxUI#createArrowButton をオーバーライドして、ArrowButtonの代わりに幅高さ0でsetVisible(false)なボタンを作成
- JComboBoxの背景色を親のJPanelと同じにして、setFocusable(false); にする

**参考リンク [#o78e1c11]
-[[Swing - Hide JComboBox Arrow?>http://forums.sun.com/thread.jspa?threadID=5337173]]

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