• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JComboBoxのBorderを変更する
#navi(../)
RIGHT:Posted by [[aterai]] at 2012-02-20
*JComboBoxのBorderを変更する [#t89b1437]
JComboBoxの表示部分、矢印ボタン、ドロップダウンリストのBorderや色を変更します。

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

//#screenshot
#ref(https://lh3.googleusercontent.com/-jHpgdiBwt6s/T0H3deyce_I/AAAAAAAABJY/_3k6-paq4lM/s800/ComboBoxBorder.png)

**サンプルコード [#ie6afc63]
#code{{
//ComboBox.border
UIManager.put("ComboBox.border", BorderFactory.createLineBorder(Color.WHITE));

//ArrowButton
combo.setUI(new BasicComboBoxUI() {
  @Override protected JButton createArrowButton() {
    JButton b = new JButton(new ArrowIcon()); //.createArrowButton();
    b.setBackground(Color.BLACK);
    b.setContentAreaFilled(false);
    b.setFocusPainted(false);
    b.setBorder(BorderFactory.createEmptyBorder());
    return b;
  }
});

//DropDownList
Object o = combo.getAccessibleContext().getAccessibleChild(0);
((JComponent)o).setBorder(BorderFactory.createMatteBorder(0,1,1,1,Color.WHITE));
}}

**解説 [#t78fddd4]
- 上: MetalComboBoxUI
-- UIManager.put("ComboBox.border", border)などで、Borderを変更しているが、UI独自の余白?を消すことができない

- 中: BasicComboBoxUI
-- MetalComboBoxUIなどにあった余白は消すことができるが、ComboBox.buttonDarkShadow がArrowButtonの三角とボタンの影に使用されているため、両方を一度に非表示にすることができない

- 下: BasicComboBoxUI#createArrowButton()
-- BasicComboBoxUI#createArrowButton()をオーバーライドして、独自のアイコンをもつJButtonを使用するように変更

----
[http://stackoverflow.com/questions/9322903/how-do-you-change-border-of-the-pop-up-section-of-a-jcombobox java - How do you change border of the pop up section of a JComboBox? - Stack Overflow] を参考にして、JComboBoxから以下のように、BasicComboPopupを取得し、Borderを設定
#code{{
Object o = combo00.getAccessibleContext().getAccessibleChild(0);
((JComponent)o).setBorder(BorderFactory.createMatteBorder(0,1,1,1,Color.WHITE));
}}

**参考リンク [#n7bf6174]
- [http://stackoverflow.com/questions/9322903/how-do-you-change-border-of-the-pop-up-section-of-a-jcombobox java - How do you change border of the pop up section of a JComboBox? - Stack Overflow]
- [JComboBoxの内余白>Swing/PaddingComboBox]

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