Swing/ComboBoxForegroundColor のバックアップの現在との差分(No.2)
TITLE:JComboBoxの文字色を変更する
Posted by aterai at 2011-02-14
JComboBoxの文字色を変更する
JComboBoxの文字色を変更します。- category: swing folder: ComboBoxForegroundColor title: JComboBoxの文字色を変更する tags: [JComboBox, ListCellRenderer, Html] author: aterai pubdate: 2011-02-14T15:46:36+09:00 description: JComboBoxに選択中のセルアイテム文字色を変更するセルレンダラーを設定します。 image:
概要
JComboBox
に選択中のセルアイテム文字色を変更するセルレンダラーを設定します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
サンプルコード
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
class ComboForegroundRenderer extends DefaultListCellRenderer {
private final Color selectionBackground = new Color(240,245,250);
private static final Color SELECTION_BACKGROUND = new Color(240, 245, 250);
private final JComboBox combo;
public ComboForegroundRenderer(JComboBox combo) {
super();
this.combo = combo;
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean hasFocus) {
if(value!=null && value instanceof ColorItem) {
#spanadd
#spanend
@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 XP
if (index < 0 && ic != null && !ic.equals(combo.getForeground())) {
combo.setForeground(ic); //Windows, Motif Look&Feel
list.setSelectionForeground(ic);
list.setSelectionBackground(selectionBackground);
list.setSelectionBackground(SELECTION_BACKGROUND);
}
JLabel l = (JLabel)super.getListCellRendererComponent(
JLabel l = (JLabel) super.getListCellRendererComponent(
list, item.description, index, isSelected, hasFocus);
l.setForeground(item.color);
l.setBackground(isSelected?selectionBackground:list.getBackground());
l.setForeground(ic);
l.setBackground(isSelected ? SELECTION_BACKGROUND
: list.getBackground());
return l;
}else{
} else {
super.getListCellRendererComponent(
list, value, index, isSelected, hasFocus);
return this;
}
}
}
解説
上記のサンプルでは、編集不可になっているJComboBoxの文字色を、選択中のアイテムから取得した色に変更するようなセルレンダラを設定しています。- Default:
- セルレンダラはデフォルト
- setForeground:
- ListCellRendererでJListの選択時文字色(JList#setSelectionForeground)、選択時背景色(JList#setSelectionBackground)を変更
- XPStyle.getXP()!=null の場合、フィールド部分の非選択時文字色は、JComboBoxの文字色(getForeground())が使用されるため、セルレンダラで、JComboBox#setForeground(Color) を使用
- html tag:
- 選択時背景色は、「setForeground:」と同様に、JList#setSelectionBackgroundを使用
- セルレンダラで文字色をHtmlタグで変更
解説
- 上:
Default
- デフォルトのリストセルレンダラーを使用
- 中:
setForeground
-
ListCellRenderer
でJList
の選択時文字色:JList#setSelectionForeground(...)
、選択時背景色:JList#setSelectionBackground(...)
を変更 -
XPStyle.getXP()!=null
なWindowsLookAndFeel
やMotifLookAndFeel
の場合フィールド部分の非選択時文字色はJComboBox
の文字色:getForeground()
が使用されるため、セルレンダラーでJComboBox#setForeground(Color)
を使用
-
- 下:
Html tag
- 選択時背景色は上記の
setForeground
と同様にJList#setSelectionBackground
を使用 - セルレンダラーで文字色を
Html
タグで変更
- 選択時背景色は上記の
#spandel
private static class ComboHtmlRenderer extends DefaultListCellRenderer {
#spanend
private final Color selectionBackground = new Color(240,245,250);
public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean isSelected, boolean hasFocus) {
#spanadd
class ComboHtmlRenderer extends DefaultListCellRenderer {
#spanend
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(selectionBackground);
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?selectionBackground:list.getBackground());
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;
}
#spanadd
#spanend
private static String hex(Color c) {
return String.format("#%06x", c.getRGB()&0xffffff);
return String.format("#%06x", c.getRGB() & 0xFF_FF_FF);
}
}