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.

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

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Attachments asia_kura.png 105.15 KB

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?

This article has been dead for over six months. Start a new discussion instead.