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 / October 2004

Tip: Looking for answers? Try searching our database.

How can I render a JList cell with JCheckBox?

Thread view: 
MaoXuePeng - 14 Oct 2004 13:27 GMT
I do it like this:

public class CheckBoxCellRenderer extends JCheckBox implements
ListCellRenderer {

 public CheckBoxCellRenderer() {
   super();
 }
 public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
   
   if(isSelected)
   {
     this.setForeground(java.awt.Color.BLUE);
     
   }
   this.setEnabled(isSelected);
   this.setText("Hello");
   return this;
 }

However, there is a problem: I can not select or disselect those check
box
rendered in the JList.
What wrong?

Thanks.
MaoXuePeng.
Babu Kalakrishnan - 14 Oct 2004 13:53 GMT
> I do it like this:
>
[quoted text clipped - 21 lines]
> rendered in the JList.
> What wrong?

Not clear what you're trying to achieve.

A Renderer is a component that is never really "present" in the
component hierarchy. When the list is being painted, the renderer is
added temporarily (not even directly - but that's an implementtion
issue) , placed at the appropriate location, is asked to paint itself,
and removed immediately thereafter. So what you see on screen is not
really the component itself - but just what it painted. You cannot
therefore expect it to react to user inputs and change state.

If what you want is an editable JList with a JCheckbox as the editor,
use a JTable with a single column, and use a JCheckBox as its
*CellEditor* (not Renderer).

BK
MaoXuePeng - 14 Oct 2004 14:31 GMT
Thank you,BK.

I want to make the check box which rendered on the JList editable, but
failed.

> So what you see on screen is not
> really the component itself - but just what it painted.  > You cannot
> therefore expect it to react to user inputs and change
> state.
> If what you want is an editable JList with a JCheckbox as > the editor,

> use a JTable with a single column, and use a JCheckBox as > its
> *CellEditor* (not Renderer).

I think you are right. I am to try it and post the answer later.

Regards!
MaoXuePeng.
Andrew Thompson - 14 Oct 2004 14:02 GMT
> I do it like this:

The 'tame' swing examples has a version that works as you want.
<http://www.physci.org/codes/tame/>

Follow the links to the launcher applet and check 'CheckListExample'
and 'CheckListExample2' to see them, follow the 'Launchet' link
from the top of the page, the drop-down menu shows the source.

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane



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.