Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

MyListCellRenderer: no highlighting anymore when item is selected

Thread view: 
Scott Steiner - 06 Jul 2005 20:51 GMT
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;
   }
}
Tom N - 07 Jul 2005 03:04 GMT
> 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
[quoted text clipped - 4 lines]
> 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.
>
[quoted text clipped - 23 lines]
>     }
> }
Tom N - 07 Jul 2005 03:08 GMT
>     public Component getListCellRendererComponent(
>         JList list,
[quoted text clipped - 5 lines]
>         super.getListCellRendererComponent( list, value, index,
>         isSelected, cellHasFocus); setText(value.toString());
       return this;
>     }
Scott Steiner - 07 Jul 2005 08:35 GMT
[...]
> It might be easier to subclass DefaultListCellRenderer and write the method (assuming value is an Icon) as...
>
[quoted text clipped - 8 lines]
>         setText(value.toString());
>     }

[...]

Thanks Tom! Subclassing DefaultListCellRenderer and calling
super.getListCellRendererComponent was the solution to my problem.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.