Summary
JComboBoxのドロップダウンリストで使用されるJListの選択背景色と選択文字色を変更します。
Screenshot

Advertisement
Source Code Examples
UIManager.put("ComboBox.selectionBackground", Color.PINK);
UIManager.put("ComboBox.selectionForeground", Color.CYAN);
String[] model = {"111", "2222", "33333"};
JComboBox<String> combo0 = new JComboBox<>(model);
JComboBox<String> combo1 = new JComboBox<String>(model) {
@Override public void updateUI() {
super.updateUI();
Object o = getAccessibleContext().getAccessibleChild(0);
if (o instanceof ComboPopup) {
JList list = ((ComboPopup) o).getList();
list.setSelectionForeground(Color.WHITE);
list.setSelectionBackground(Color.ORANGE);
}
}
};
JComboBox<String> combo2 = new JComboBox<String>(model) {
@Override public void updateUI() {
super.updateUI();
setRenderer(new DefaultListCellRenderer() {
@Override public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean isSelected, boolean hasFocus) {
JLabel l = (JLabel) super.getListCellRendererComponent(
list, value, index, isSelected, hasFocus);
if (isSelected) {
l.setForeground(Color.WHITE);
l.setBackground(Color.ORANGE);
} else {
l.setForeground(Color.BLACK);
l.setBackground(Color.WHITE);
}
return l;
}
});
}
};
View in GitHub: Java, KotlinDescription
UIManager.put(ComboBox.selection*, ...)UIManager.put("ComboBox.selectionBackground", bgc);、UIManager.put("ComboBox.selectionForeground", fgc);を使用してJListの選択背景色と選択文字色を変更- ドロップダウンリストで使用される
JListのみが対象 LookAndFeelに依存し、例えばNimbusLookAndFeelではどちらの指定も無効
ComboPopup.getList().setSelection*(...)JComboBox#getAccessibleContext()#getAccessibleChild(0)でComboPopupを取得ComboPopup#getList()メソッドでドロップダウンリストで使用されるJListを取得JList#setSelectionForeground(...)、JList#setSelectionBackground(...)メソッドを使用してその選択背景色と選択文字色を変更MetalLookAndFeelで編集不可のJComboBoxの場合JComboBox本体の選択背景色も変更される
DefaultListCellRenderer- 参考: JComboBoxの文字色を変更する
DefaultListCellRenderer#getListCellRendererComponent(...)メソッドをオーバーライドして選択背景色と選択文字色を変更MetalLookAndFeelで編集不可のJComboBoxの場合JComboBox本体の選択背景色は変更されない- このサンプルの場合ドロップダウンリストの選択背景色に設定したオレンジ色ではなく
UIManager.put("ComboBox.selectionBackground", Color.PINK)で設定したピンク色になる
- このサンプルの場合ドロップダウンリストの選択背景色に設定したオレンジ色ではなく
JComboBox本体のフォーカス時のBorder(WindowsLookAndFeelの場合はWindowsBorders.DashedBorder)が非表示になる- 回避方法: セルの描画を
DefaultListCellRendererを継承するレンダラーではなくJComboBox#getRenderer()で取得したLookAndFeelのデフォルトセルレンダラーに移譲するなどの方法があるJComboBox<String> combo3 = new JComboBox<String>(model) { @Override public void updateUI() { setRenderer(null); super.updateUI(); ListCellRenderer<? super String> defaultRenderer = getRenderer(); setRenderer(new ListCellRenderer<String>() { @Override public Component getListCellRendererComponent( JList<? extends String> list, String value, int index, boolean isSelected, boolean hasFocus) { JLabel l = (JLabel) defaultRenderer.getListCellRendererComponent( list, value, index, isSelected, hasFocus); if (isSelected) { l.setForeground(Color.WHITE); l.setBackground(Color.ORANGE); } else { l.setForeground(Color.BLACK); l.setBackground(Color.WHITE); } return l; } }); } };
- 回避方法: セルの描画を