---
category: swing
folder: ComboBoxForegroundColor
title: JComboBoxの文字色を変更する
tags: [JComboBox, ListCellRenderer, Html]
author: aterai
pubdate: 2011-02-14T15:46:36+09:00
description: JComboBoxに選択中のセルアイテム文字色を変更するセルレンダラーを設定します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TVjIM1AMFkI/AAAAAAAAA1M/BSd3As9dxZE/s800/ComboBoxForegroundColor.png
---
* Summary [#summary]
`JComboBox`に選択中のセルアイテム文字色を変更するセルレンダラーを設定します。
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TVjIM1AMFkI/AAAAAAAAA1M/BSd3As9dxZE/s800/ComboBoxForegroundColor.png)
* Source Code Examples [#sourcecode]
#code(link){{
class ComboForegroundRenderer extends DefaultListCellRenderer {
private static final Color SELECTION_BACKGROUND = new Color(240, 245, 250);
private final JComboBox combo;
public ComboForegroundRenderer(JComboBox combo) {
super();
this.combo = combo;
}
@Override public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean isSelected, boolean hasFocus) {
if (value instanceof ColorItem) {
ColorItem item = (ColorItem) value;
Color ic = item.color;
if (index < 0 && ic != null && !ic.equals(combo.getForeground())) {
combo.setForeground(ic); //Windows, Motif Look&Feel
list.setSelectionForeground(ic);
list.setSelectionBackground(SELECTION_BACKGROUND);
}
JLabel l = (JLabel) super.getListCellRendererComponent(
list, item.description, index, isSelected, hasFocus);
l.setForeground(ic);
l.setBackground(isSelected ? SELECTION_BACKGROUND
: list.getBackground());
return l;
} else {
super.getListCellRendererComponent(
list, value, index, isSelected, hasFocus);
return this;
}
}
}
}}
* Description [#explanation]
* Description [#description]
- 上: `Default`
-- デフォルトのリストセルレンダラーを使用
- 中: `setForeground`
-- `ListCellRenderer`で`JList`の選択時文字色:`JList#setSelectionForeground(...)`、選択時背景色:`JList#setSelectionBackground(...)`を変更
-- `XPStyle.getXP()!=null`な`WindowsLookAndFeel`や`MotifLookAndFeel`の場合フィールド部分の非選択時文字色は`JComboBox`の文字色:`getForeground()`が使用されるため、セルレンダラーで`JComboBox#setForeground(Color)`を使用
- 下: `Html tag`
-- 選択時背景色は上記の`setForeground`と同様に`JList#setSelectionBackground`を使用
-- セルレンダラーで文字色を`Html`タグで変更
#code{{
class ComboHtmlRenderer extends DefaultListCellRenderer {
private static final Color SELECTION_BACKGROUND = new Color(240, 245, 250);
@Override public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean isSelected, boolean hasFocus) {
ColorItem item = (ColorItem) value;
if (index < 0) {
list.setSelectionBackground(SELECTION_BACKGROUND);
}
JLabel l = (JLabel) super.getListCellRendererComponent(
list, value, index, isSelected, hasFocus);
l.setText("<html><font color=" + hex(item.color) + ">" + item.description);
l.setBackground(isSelected ? SELECTION_BACKGROUND : list.getBackground());
return l;
}
private static String hex(Color c) {
return String.format("#%06x", c.getRGB() & 0xFF_FF_FF);
}
}
}}
* Reference [#reference]
- [[JComboBoxの色を変更>Swing/ColorComboBox]]
* Comment [#comment]
#comment
#comment