Article View: borland.public.jbuilder.java.swing
Article #1174Re: Someone have example jList ListCellRenderer example?
From: nospam
Date: Sun, 21 Mar 2004 01:04
Date: Sun, 21 Mar 2004 01:04
136 lines
4588 bytes
4588 bytes
On Sat, 20 Mar 2004 13:55:28 -0800, John McGrath [TeamB] wrote:
> On 3/20/2004 at 8:28:45 AM, nospam wrote:
>
>> [quoted text muted]
>
>
>> [quoted text muted]
> A cell renderer is an object that provides a Component that will be used
> to do the painting. Normally, the getListCellRendererComponent() method
> will configure the properties of the Component so it will render the cell
> appropriately.
>
> As is often the case, this cell renderer returns the same component each
> time getListCellRendererComponent() is called. Since the above code
> configures the "Selected" property only if cellHasFocus is true, it will
> be left in whatever state it was in previous time it was used.
>
> So you need to change the code to set the "Selected" property even when
> "cellHasFocus" is false.
>
> if (cellHasFocus) {
> if (isSelected) {
> setSelected(false);
> }
> else {
> setSelected(true);
> }
> }
> else {
> setSelected(true);
> }
> }
> Of course, these 11 lines of code could be replace with the following
> single line:
>
> setSelected( cellHasFocus & !isSelected );
>
> This is much simpler and more efficient and it also makes it far more
> clear that the "Selected" property will always be set.
>
>> [quoted text muted]
>
> BTW, many people frown on anonymous messages. I can understand why you
> might want to omit your e-mail address, but I can think of no reason to
> omit your name.
Thank you for your response. I tried what you suggested,
and it still does not work. Here is my entire renderer
class. I've commented the code you provided below, and
what it does.
Thanks
-Bernie
public class CheckListRenderer extends JCheckBox implements ListCellRenderer {
int numSelected = 0;
public CheckListRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
setText(value.toString());
setBackground(isSelected ? Color.blue : Color.lightGray);
setForeground(isSelected ? Color.white : Color.black);
//THIS ISN'T CHECKING THE CHECKBOXES
setSelected( cellHasFocus & !isSelected );
//THIS CHECKS ALL CHECKBOXES INITIALLY, BUT DOESN'T UNCHECK.
// if (cellHasFocus) {
// if (isSelected) {
// setSelected(false);
// }
// else {
// setSelected(true);
// }
// }
// else {
// setSelected(true);
// }
System.out.println(value.toString() + " is selected");
// System.out.println("index = " + String.valueOf(index) );
// else // Keep CheckBox the same....
// {
// if (isSelected()) {
// setSelected(false);
// }
// else {
// setSelected(false);
// }
// }
// if (isSelected) {
// setSelected(false);
// }
// else {
// setSelected(true);
// }
return this;
}
}
Technical Details
Message-ID:
<pan.2004.03.21.01.04.02.13739@spam.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<pan.2004.03.17.02.25.29.868166@spam.com> <405a7347@newsgroups.borland.com> <pan.2004.03.20.04.55.47.68889@spam.com> <405bed9f@newsgroups.borland.com> <pan.2004.03.20.13.28.44.699216@spam.com> <405cbdd0@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,de66bf01ed98544f X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2004-03-20 17:10:04 PST From: nospam <no@spam.com> Subject: Re: Someone have example jList ListCellRenderer example? Date: Sat, 20 Mar 2004 20:04:02 -0500 User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) Message-ID: <pan.2004.03.21.01.04.02.13739@spam.com> Newsgroups: borland.public.jbuilder.java.swing References: <pan.2004.03.17.02.25.29.868166@spam.com> <405a7347@newsgroups.borland.com> <pan.2004.03.20.04.55.47.68889@spam.com> <405bed9f@newsgroups.borland.com> <pan.2004.03.20.13.28.44.699216@spam.com> <405cbdd0@newsgroups.borland.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit NNTP-Posting-Host: 68.81.92.143 X-Trace: newsgroups.borland.com 1079831039 68.81.92.143 (20 Mar 2004 17:03:59 -0800) Lines: 136 Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail