🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
9 messages
9 total messages Started by nospam Wed, 17 Mar 2004 02:25
Someone have example jList ListCellRenderer example?
#1144
Author: nospam
Date: Wed, 17 Mar 2004 02:25
6 lines
1132 bytes
I'm trying to create a ListCellRenderer using a jList.
I simply want a checkbox next to each item in the list,
and the ability to check or uncheck the checkboxes.
Also, highlighting the list entry would be nice.

-Thanks
Re: Someone have example jList ListCellRenderer example?
#1145
Author: pnichols
Date: Wed, 17 Mar 2004 07:09
16 lines
1365 bytes
nospam wrote:

> I'm trying to create a ListCellRenderer using a jList.
> I simply want a checkbox next to each item in the list,
> and the ability to check or uncheck the checkboxes.
> Also, highlighting the list entry would be nice.
> 
> -Thanks

Here's a pretty good site:

http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/

This one may be exactly what you are looking for:

http://codeguru.earthweb.com/java/articles/567.shtml
Re: Someone have example jList ListCellRenderer example?
#1150
Author: John McGrath [TeamB]
Date: Fri, 19 Mar 2004 04:12
17 lines
1425 bytes
On 3/16/2004 at 9:25:29 PM, nospam wrote:

> I simply want a checkbox next to each item in the list,
> and the ability to check or uncheck the checkboxes.
> Also, highlighting the list entry would be nice.

I have a CheckList class that I wrote some time ago.  
I posted a copy of it to the .attachments newsgroup.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Re: Someone have example jList ListCellRenderer example?
#1166
Author: nospam
Date: Sat, 20 Mar 2004 04:55
27 lines
1742 bytes
On Thu, 18 Mar 2004 20:12:55 -0800, John McGrath  [TeamB] wrote:

> On 3/16/2004 at 9:25:29 PM, nospam wrote:
> 
>> [quoted text muted]
> 
> I have a CheckList class that I wrote some time ago. I posted a copy of it
> to the .attachments newsgroup.


Thank you for the code.  Can you please tell me
why the following does not behave as expected
in my ListCellRenderer?



   if (cellHasFocus) {

       if (isSelected == false) {
             setSelected(true);
         }

       if (isSelected == true) {
               setSelected(false);
        }

   }
Re: Someone have example jList ListCellRenderer example?
#1167
Author: John McGrath [TeamB]
Date: Sat, 20 Mar 2004 07:07
19 lines
1479 bytes
On 3/19/2004 at 11:55:47 PM, nospam wrote:

> Can you please tell me why the following does not behave 
> as expected in my ListCellRenderer?

No, because you have not indicated:

1) What the code does.
2) What you expect it to do.
3) The context for the code.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Re: Someone have example jList ListCellRenderer example?
#1170
Author: nospam
Date: Sat, 20 Mar 2004 13:28
58 lines
2418 bytes
On Fri, 19 Mar 2004 23:07:11 -0800, John McGrath  [TeamB] wrote:

> On 3/19/2004 at 11:55:47 PM, nospam wrote:
> 
>> [quoted text muted]
> 
> No, because you have not indicated:
> 
> 1) What the code does.
> 2) What you expect it to do.
> 3) The context for the code.

The code is in my ListCellRenderer.  I would expect it to either
check or uncheck a Jcheckbox in the Jlist, if it is clicked, and
if it has focus.  The best I can to is check a box, but not
uncheck it.  I've tried NUMEROUS permutations of this.

-Thanks



public class CheckListRenderer  extends JCheckBox implements ListCellRenderer {


  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);


//WHY DOESN'T THIS CHECK/UNCKECK CHECKBOXES???

   if (cellHasFocus) {
       if (isSelected) {
            setSelected(false);
         }
        else {
            setSelected(true);
         }

     System.out.print("GOT HERE");
  }



  return this;
}
Re: Someone have example jList ListCellRenderer example?
#1173
Author: John McGrath [TeamB]
Date: Sat, 20 Mar 2004 21:55
64 lines
2902 bytes
On 3/20/2004 at 8:28:45 AM, nospam wrote:

> The best I can to is check a box, but not uncheck it.
> I've tried NUMEROUS permutations of this.


>    if (cellHasFocus) {
>        if (isSelected) {
>             setSelected(false);
>          }
>         else {
>             setSelected(true);
>          }
> 
>      System.out.print("GOT HERE");
>   }

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.

> From: nospam <no@spam.com>

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.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Re: Someone have example jList ListCellRenderer example?
#1174
Author: nospam
Date: Sun, 21 Mar 2004 01:04
136 lines
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;
     }





}
Re: Someone have example jList ListCellRenderer example?
#1175
Author: John McGrath [TeamB]
Date: Mon, 22 Mar 2004 08:33
31 lines
2372 bytes
On 3/20/2004 at 8:04:02 PM, nospam wrote:

> Thank you for your response.  I tried what you suggested,
> and it still does not work.  

Now it should show the checkbox with a check mark in the current cell
when it is not selected.  That certainly is pretty weird, but your
original code was written that way.  In order to get it into a state
where it will show the check-mark, you will have to control-click on
the current cell.

It seems as if you are somehow expecting the selection state of the
JList to be used as the selection state for the virtual checkboxes you
are creating in the list.  That will not work, or at least not very
well.  You obviously need a model in which to store the selection state
for the checkboxes.

Have you looked at the code that I provided?  I think it does exactly
what you have asked for, yet you seem to be trying to write something
completely different.  If you think it is not what you need, please
tell us why.  If you understood what my code does, you should
understand the issues here.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads