Swing/ComboBoxForegroundColor のバックアップソース(No.2)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/ComboBoxForegroundColor へ行く。
- 1 (2011-03-15 (火) 14:10:48)
- 2 (2011-03-24 (木) 16:28:09)
- 3 (2012-02-07 (火) 16:47:26)
- 4 (2012-12-23 (日) 05:41:16)
- 5 (2014-02-19 (水) 02:29:49)
- 6 (2014-02-23 (日) 19:50:46)
- 7 (2014-11-15 (土) 00:49:52)
- 8 (2015-12-12 (土) 02:18:33)
- 9 (2017-03-08 (水) 13:02:03)
- 10 (2018-01-10 (水) 18:02:32)
- 11 (2018-12-21 (金) 14:07:43)
- 12 (2020-11-19 (木) 14:40:15)
- 13 (2023-01-06 (金) 17:32:14)
TITLE:JComboBoxの文字色を変更する #navi(../) RIGHT:Posted by [[aterai]] at 2011-02-14 *JComboBoxの文字色を変更する [#v4a48c3a] JComboBoxの文字色を変更します。 -&jnlp; -&jar; -&zip; //#screenshot #ref(https://lh4.googleusercontent.com/_9Z4BYR88imo/TVjIM1AMFkI/AAAAAAAAA1M/BSd3As9dxZE/s800/ComboBoxForegroundColor.png) **サンプルコード [#f2e9b786] #code{{ class ComboForegroundRenderer extends DefaultListCellRenderer { private final Color selectionBackground = new Color(240,245,250); private final JComboBox combo; public ComboForegroundRenderer(JComboBox combo) { this.combo = combo; } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) { if(value!=null && 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 list.setSelectionForeground(ic); list.setSelectionBackground(selectionBackground); } JLabel l = (JLabel)super.getListCellRendererComponent( list, item.description, index, isSelected, hasFocus); l.setForeground(item.color); l.setBackground(isSelected?selectionBackground:list.getBackground()); return l; }else{ super.getListCellRendererComponent( list, value, index, isSelected, hasFocus); return this; } } } }} **解説 [#x50dce7e] 上記のサンプルでは、編集不可になっているJComboBoxの文字色を、選択中のアイテムから取得した色に変更するようなセルレンダラを設定しています。 -Default: --セルレンダラはデフォルト -setForeground: --ListCellRendererでJListの選択時文字色(JList#setSelectionForeground)、選択時背景色(JList#setSelectionBackground)を変更 --XPStyle.getXP()!=null の場合、フィールド部分の非選択時文字色は、JComboBoxの文字色(getForeground())が使用されるため、セルレンダラで、JComboBox#setForeground(Color) を使用 -html tag: --選択時背景色は、「setForeground:」と同様に、JList#setSelectionBackgroundを使用 --セルレンダラで文字色をHtmlタグで変更 #code{{ private static class ComboHtmlRenderer extends DefaultListCellRenderer { private final Color selectionBackground = new Color(240,245,250); public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean hasFocus) { ColorItem item = (ColorItem) value; if(index<0) { list.setSelectionBackground(selectionBackground); } 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()); return l; } private static String hex(Color c) { return String.format("#%06x", c.getRGB()&0xffffff); } } }} **参考リンク [#h54da1c1] -[[JComboBoxの色を変更>Swing/ColorComboBox]] **コメント [#ea073c97] #comment