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.

Recommended Answers

All 4 Replies

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

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

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.