setBackground in ComboBoxes

Discussion in 'Java' started by Thomas Richter, Nov 12, 2003.

  1. Hi,

    is there a known problem with custom renderers in JComboBoxes?

    I've here the following problem:

    Given a class derived from the swing JComboBox with a custom
    renderer given as follows

    class ColorComboBoxRenderer implements ListCellRenderer {
    /* snip */
    public Component getListCellRendererComponent(JList list,
    Object value,int index,
    boolean isSelected,
    boolean cellHasFocus) {
    ColorLabel label = (ColorLabel)value;
    //
    if (isSelected) {
    label.setBackground(list.getSelectionBackground());
    label.setForeground(list.getSelectionForeground());
    } else {
    label.setBackground(list.getBackground());
    label.setForeground(list.getForeground());
    }
    // We should maybe render the selected item in a nicer way...
    return label;
    }

    and a ColorLabel class that is directly derived from the JLabel, I get
    no visible feedback at all if the user moves the pointer over the elements
    of the ComboBox. However, I can make sure (by a couple of "println"'s) that
    the code arrives at the right spot (above) and that the colors feed into
    the label class are, as such, correct.

    Any idea what's going on here? Is it impossible to change the background
    color of a JLabel in a custom renderer of a JComboBox?

    Greetings,
    Thomas
    Thomas Richter, Nov 12, 2003
    #1
    1. Advertising

  2. Thomas Richter

    VisionSet Guest

    "Thomas Richter" <-berlin.de> wrote in message
    news:botp2f$doj$-Berlin.DE...

    >
    > Any idea what's going on here? Is it impossible to change the background
    > color of a JLabel in a custom renderer of a JComboBox?
    >


    In a rush, so haven't read your post properly.
    IIRC JLabels are transparent by default and inherit the background of their
    parent, setOpaque(true) and it will work.

    --
    Mike W
    VisionSet, Nov 12, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith Henderson

    for each for comboboxes on page

    Keith Henderson, Sep 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    363
    Scott Allen
    Sep 24, 2004
  2. Jenny
    Replies:
    2
    Views:
    41,019
    Zeptune
    May 9, 2014
  3. Jenny

    Graphic2D.setBackground

    Jenny, Aug 25, 2004, in forum: Java
    Replies:
    6
    Views:
    5,209
    Babu Kalakrishnan
    Aug 30, 2004
  4. Russell Wallace

    JTabbedPane ignoring setBackground

    Russell Wallace, Mar 10, 2005, in forum: Java
    Replies:
    6
    Views:
    1,031
    Russell Wallace
    Mar 10, 2005
  5. ownowl
    Replies:
    6
    Views:
    615
    Roedy Green
    Jul 11, 2009
Loading...

Share This Page