Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is another approach:</p> <pre><code>//JList#setSelectionForeground(...) version static class ColoredCellRenderer implements ListCellRenderer { protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer(); private final Color selectionBackground = new Color(240,200,200); public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Color fgc = Colors.valueOf((String)value).color; if(index&lt;0) { //comboBox.setForeground(fgc); //Windows, CDE/Motif Look &amp; Feel list.setSelectionForeground(fgc); list.setSelectionBackground(selectionBackground); } JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus); if (index != -1) { renderer.setForeground(fgc); } return renderer; } } //html version static class ComboHtmlRenderer extends DefaultListCellRenderer { private final Color selectionBackground = new Color(240,200,200); @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean hasFocus) { Color fgc = Colors.valueOf((String)value).color; if(index&lt;0) { list.setSelectionBackground(selectionBackground); } JLabel l = (JLabel)super.getListCellRendererComponent( list, value, index, isSelected, hasFocus); l.setText("&lt;html&gt;&lt;font color="+hex(fgc)+"&gt;"+value); l.setBackground(isSelected?selectionBackground:list.getBackground()); return l; } private static String hex(Color c) { return String.format("#%06x", c.getRGB()&amp;0xffffff); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload