aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help implementing DefaultListCellRenderer. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help implementing DefaultListCellRenderer." Watch "Help implementing DefaultListCellRenderer." New topic
Author

Help implementing DefaultListCellRenderer.

Matthew Fierro
Greenhorn

Joined: Mar 20, 2010
Posts: 16
I need help implementing a ListCellRenderer such that based on weather or not a particular value exists within an individual list item, that particular row should be colored.

I create a JList as follows, set the Cell Renderer, I also add a mouse listener and then I add the list to a JScrollPane




I then do the following to populate the JList.




So now I want to check the 8th list item value for each list item to see if it contains the value "Yes"

I make an attempt to use a DefaultListCellRenderer but I am not quite sure how to use it to do what I want.




Currently I get no errors but the list displays garbled information and none of the list items are being colored.

















[Thumbnail for gmimg.jpg]

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

You're passing the JList as the value parameter to the super implementation. What you see is the toString() output of the JList.

Passing 8 as the index parameter isn't correct either.


luck, db
There are no new questions, but there may be new answers.
Matthew Fierro
Greenhorn

Joined: Mar 20, 2010
Posts: 16
Thanks for the reply. I relize that I am doing this wrong. I was looking for an example of how to implement this correctly.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

Take a break, think about it some, then try again.
Matthew Fierro
Greenhorn

Joined: Mar 20, 2010
Posts: 16
I am still not there yet but I feel like I am getting close. Here is my new CellRenderer class. Currently all list items are still not colored.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

Yes, you're much closer, but you do need to use consistent indents in your code so that the nesting of loops and conditional blocks can be clearly seen. Refer to http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

Why do you think you need to loop over all the items inside that method? The method will be called by the Swing core code for each index, in order to obtain the renderer for that cell.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Never use == to compare Strings, always use .equals.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matthew Fierro
Greenhorn

Joined: Mar 20, 2010
Posts: 16
Ok so I have cleaned up the class a little but still no luck.


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
worked OK for me --> changed the text color to red.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

Your list is populated with objects of type MusicRecording. Can the item being rendered (the value parameter) ever be equal to the String "Rare"?
Matthew Fierro
Greenhorn

Joined: Mar 20, 2010
Posts: 16
Well, attached is a sample output. As you can see in this example, there are two items in the list. They are being pulled in from an Oracle DB. Looking at the first list item, the second value is "Rare". So for this list item and all future list items where the second value is "Rare", is when I want to assign a different foreground color.


[Thumbnail for My Game Collection_2012-05-16_04-50-08.png]

Matthew Fierro
Greenhorn

Joined: Mar 20, 2010
Posts: 16
OK, so I got it working for th evalue "Rare" and also for the value "true" as seen in the syntax below. However, I would like to have it so that the value "true" is passed to the JList but not actually displayed. Currently it is part of my toString() method. Is there a way to have a value on the list but not actually display it?




The Music Recording Class containing the toString()






Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

Matthew Fierro wrote:Is there a way to have a value on the list but not actually display it?


Sure there is! Your list is populated with instances of MusicRecording, and those instances are available in the renderer method as the value parameter. After casting value to MusicRecording, you can call any method of the class.

The displayed String defaults to the one returned by toString(), but there's no reason a custom renderer shouldn't use any other String, normally but not compulsorily obtained from value
Matthew Fierro
Greenhorn

Joined: Mar 20, 2010
Posts: 16
I am not sure what you mean by

After casting value to MusicRecording, you can call any method of the class.



Currenty I am doing


ArrayList<MusicRecording> info = myDataAccessor.getRecordings(category);


musicListBox.setListData(info.toArray());

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

Matthew Fierro
Greenhorn

Joined: Mar 20, 2010
Posts: 16
Thank You Very Much. It is working beutifully now.
Matthew Fierro
Greenhorn

Joined: Mar 20, 2010
Posts: 16
What I do after that is even better. When they click on the item, a new JPanel opens. On it is an embeded VLC player which streams youtube videos. It pulls the link from my Oracle database and stream the video on the screen. I also have a description in a JTextArea which is populated from a text file. Here it is attached.



[Thumbnail for mariogame.jpg]

Matthew Fierro
Greenhorn

Joined: Mar 20, 2010
Posts: 16
Sorry to "re-open" this but there is something else I would like to do. If the list is empty, I would like to display an image centered within the JList. I have tried some things below but it is not working.


I added the following to MyListRenderer class.




In my actionListener I do the following.




Essentially what I am trying to do is if the list is empty I want to remove that list since it holds MusicRecording objects and I want to add a new list that holds images. Of course if a selection is made where there is data, I want to remove the image list and put of the list with the data items from the Music Recording.





Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

JList (in a JScrollPane) and JLabel (with an ImageIcon) in a CardLayout
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help implementing DefaultListCellRenderer.