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 / GUI / November 2003

Tip: Looking for answers? Try searching our database.

Writing a ListCellRenderer

Thread view: 
kelvSYC - 28 Nov 2003 06:43 GMT
I'm writing my own ListCellRenderer for a JList, and I seem to be
running into a problem.

The code below (in getListCellRendererComponent) is supposed to (or so
I am told) appropriately change the color of the selected item so it
stands out (and conforms to a user's preferences, etc).  However, when
I actually set the JList's ListCellRenderer to my custom renderer,
nothing happens to the background color or the foreground color.  Can
anyone explain why?

     if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
     } else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
     }

Signature

I am only a mirage.

ak - 28 Nov 2003 16:43 GMT
please post complete getListCellRendererComponent() method.

--

____________

http://reader.imagero.com the best java image reader.

> I'm writing my own ListCellRenderer for a JList, and I seem to be
> running into a problem.
[quoted text clipped - 16 lines]
> --
> I am only a mirage.
Lee Weiner - 28 Nov 2003 17:06 GMT
>I'm writing my own ListCellRenderer for a JList, and I seem to be
>running into a problem.
[quoted text clipped - 13 lines]
>         setForeground(list.getForeground());
>      }

Without seeing the whole method I'm only guessing, but I'll bet you didn't set
the label to be opaque.

       setOpaque( true );

Lee Weiner
lee AT leeweiner DOT org
kelvSYC - 29 Nov 2003 02:10 GMT
> Without seeing the whole method I'm only guessing, but I'll bet you didn't
> set the label to be opaque.

That worked!  But what does opacity do?

Signature

I am only a mirage.

Lee Weiner - 29 Nov 2003 02:24 GMT
>> Without seeing the whole method I'm only guessing, but I'll bet you didn't
>> set the label to be opaque.
>
>That worked!  But what does opacity do?

A ListCellRenderer is basically a JLabel.  The background of a JLabel is, by
default, transparent and doesn't display any color of its own, it displays the
background color of the component it's in.  Setting it to be opaque allows it
to display its own background color.

Lee Weiner
lee AT leeweiner DOT org


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.