• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help implementing DefaultListCellRenderer.

 
Matthew Fierro
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.














gmimg.jpg
screenshot
[Thumbnail for gmimg.jpg]
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matthew Fierro
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a break, think about it some, then try again.
 
Matthew Fierro
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use == to compare Strings, always use .equals.
 
Matthew Fierro
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I have cleaned up the class a little but still no luck.


 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
worked OK for me --> changed the text color to red.
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
My Game Collection_2012-05-16_04-50-08.png
[Thumbnail for My Game Collection_2012-05-16_04-50-08.png]
 
Matthew Fierro
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Matthew Fierro
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Very Much. It is working beutifully now.
 
Matthew Fierro
Greenhorn
Posts: 16
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
mariogame.jpg
Example
[Thumbnail for mariogame.jpg]
 
Matthew Fierro
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JList (in a JScrollPane) and JLabel (with an ImageIcon) in a CardLayout
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic