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
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.
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
posted
0
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.
Matthew Fierro
Greenhorn
Joined: Mar 20, 2010
Posts: 16
posted
0
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()
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
posted
0
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);
Thank You Very Much. It is working beutifully now.
Matthew Fierro
Greenhorn
Joined: Mar 20, 2010
Posts: 16
posted
1
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.
Matthew Fierro
Greenhorn
Joined: Mar 20, 2010
Posts: 16
posted
0
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.