MyListCellRenderer: no highlighting anymore when item is selected

Discussion in 'Java' started by Scott Steiner, Jul 6, 2005.

  1. Hi,

    I need a JList that displays items which consist of an image and some
    text. For this purpose I wrote my own ListCellRenderer which did the
    trick. However, I'm now having a problem with the selection colors. When
    an item is selected then the text disappears and only the image is
    visible. If I don't use MyListCellRenderer then everything is fine and
    the selected item gets highlighted in the windows default blue, but as
    soon as MyListCellRenderer is set as the default cell renderer then no
    highlighting is there.

    Any help appreciated.

    public class MyListCellRenderer extends JLabel implements
    ListCellRenderer
    {
    public Component getListCellRendererComponent(
    JList list,
    Object value,
    int index,
    boolean isSelected,
    boolean cellHasFocus)
    {
    if (isSelected)
    {
    setBackground(list.getSelectionBackground());
    setForeground(list.getSelectionForeground());
    }
    else
    {
    setBackground(list.getBackground());
    setForeground(list.getForeground());
    }
    setText(value.toString());
    setIcon(new ImageIcon(value));
    return this;
    }
    }
     
    Scott Steiner, Jul 6, 2005
    #1
    1. Advertising

  2. Scott Steiner

    Tom N Guest

    Scott Steiner wrote:

    > I need a JList that displays items which consist of an image and some
    > text. For this purpose I wrote my own ListCellRenderer which did the
    > trick. However, I'm now having a problem with the selection colors. When
    > an item is selected then the text disappears and only the image is
    > visible. If I don't use MyListCellRenderer then everything is fine and
    > the selected item gets highlighted in the windows default blue, but as
    > soon as MyListCellRenderer is set as the default cell renderer then no
    > highlighting is there.


    Have you checked what the colours are that you are getting from list.getSelectionBackground() and
    list.getSelectionForeground()?

    It might be worth hard-coding some colours for testing purposes as the list might be supplying colours which are
    the same for FG and BG.

    The DefaultListCellRenderer doesn't do much different from your code except at the start it does...
    setComponentOrientation(list.getComponentOrientation());
    and at the end it does...
    setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);

    and it does this in the constructor...
    noFocusBorder = new EmptyBorder(1, 1, 1, 1);
    setOpaque(true);
    setBorder(noFocusBorder);

    I can't see any of that affecting colours except perhaps the setOpaque().

    It might be easier to subclass DefaultListCellRenderer and write the method (assuming value is an Icon) as...

    public Component getListCellRendererComponent(
    JList list,
    Object value,
    int index,
    boolean isSelected,
    boolean cellHasFocus)
    {
    super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus);
    setText(value.toString());
    }


    > Any help appreciated.
    >
    > public class MyListCellRenderer extends JLabel implements
    > ListCellRenderer
    > {
    > public Component getListCellRendererComponent(
    > JList list,
    > Object value,
    > int index,
    > boolean isSelected,
    > boolean cellHasFocus)
    > {
    > if (isSelected)
    > {
    > setBackground(list.getSelectionBackground());
    > setForeground(list.getSelectionForeground());
    > }
    > else
    > {
    > setBackground(list.getBackground());
    > setForeground(list.getForeground());
    > }
    > setText(value.toString());
    > setIcon(new ImageIcon(value));
    > return this;
    > }
    > }
     
    Tom N, Jul 7, 2005
    #2
    1. Advertising

  3. Scott Steiner

    Tom N Guest

    Tom N wrote:

    > public Component getListCellRendererComponent(
    > JList list,
    > Object value,
    > int index,
    > boolean isSelected,
    > boolean cellHasFocus)
    > {
    > super.getListCellRendererComponent( list, value, index,
    > isSelected, cellHasFocus); setText(value.toString());

    return this;
    > }
     
    Tom N, Jul 7, 2005
    #3
  4. Tom N wrote:
    [...]
    > It might be easier to subclass DefaultListCellRenderer and write the method (assuming value is an Icon) as...
    >
    > public Component getListCellRendererComponent(
    > JList list,
    > Object value,
    > int index,
    > boolean isSelected,
    > boolean cellHasFocus)
    > {
    > super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus);
    > setText(value.toString());
    > }
    >

    [...]

    Thanks Tom! Subclassing DefaultListCellRenderer and calling
    super.getListCellRendererComponent was the solution to my problem.
     
    Scott Steiner, Jul 7, 2005
    #4
    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. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    Highlighting Selected row on Edit Mode

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Nov 14, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    364
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Nov 15, 2004
  2. =?Utf-8?B?c2F0aXNo?=

    Highlighting the selected node in treeview control

    =?Utf-8?B?c2F0aXNo?=, Oct 21, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    4,804
    Patrick.O.Ige
    Oct 22, 2005
  3. Iain
    Replies:
    3
    Views:
    939
  4. Eric Lilja
    Replies:
    4
    Views:
    486
  5. mldardy
    Replies:
    0
    Views:
    972
    mldardy
    Sep 28, 2010
Loading...

Share This Page