954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Updating a JList Renderer

Note: I'm using a JList as an audio playlist.

I was wondering if there was a way to automatically update a custom ListCellRenderer. Basically I ask is because within my ListCellRenderer I have an isPlaying item that will change the color of the object in my playlist that is playing, however when the next file begins to play the previous item that was playing will remain the playing color until I highlight it with the focus and then the CellRenderer updates it. Hope I'm explaining this enough to be understood. Maybe you have another idea to go about what I'm looking to do.

Frank0051
Light Poster
27 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

You're going to have to do something with the ListModel and maybe create a custom renderer as well.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Well I guess for now I will just pass the focus onto the current playing file and then not let the user be able to move it; then in the future I will work out that feature

Frank0051
Light Poster
27 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

I think, i resolved your old problem.This allow independent highlighting indicated(YELLOW) index and selected indexes(CYAN) without using the focus system.
1. from java doc:
javax.swing.DefaultListSelectionModel
Added Methods. void moveLeadSelectionIndex( int ), Set the lead selection index, leaving all selection values unchanged. ...
2. defs

public static JList anyList;
    static {
        DefaultListModel anyModel = new DefaultListModel();
        anyList = new JList(anyModel);
        anyList.setSelectionModel(new DefaultListSelectionModel());
        anyList.setCellRenderer(new MyRenderer());
    // ... filling anyList
    }

3. impl.

import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JList;

public class MyRenderer extends javax.swing.JLabel implements javax.swing.ListCellRenderer {

    public java.awt.Component getListCellRendererComponent(javax.swing.JList list, java.lang.Object value, int index, boolean isSelected, boolean cellHasFocus) {
        // ...        
        if (isSelected) {
            setBackground(java.awt.Color.CYAN);
            setForeground(java.awt.Color.BLACK);
        }
        if (((DefaultListSelectionModel) anyList.getSelectionModel()).getLeadSelectionIndex() == index) {
            setBackground(java.awt.Color.YELLOW);
            setForeground(java.awt.Color.BLACK);
        }
        // ...
        return this;
    }
}

4. use - set indicated index for example 5:

int index = 5; // 
((DefaultListSelectionModel) anyList.getSelectionModel()).moveLeadSelectionIndex(index);

quuba

Attachments asia_kura.png 105.15KB
quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 123
Solved Threads: 106
 
I think, i resolved your old problem.

old problem to say the least. is there any reason you have to assume that during the last two years he/she didn't manage to find the sollution him/herself?

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: