このSSCCEは私のために働きます:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class BlinkingLabelInList extends JPanel {
public static final Color FLASH_COLOR = Color.red;
public static final int TIMER_DELAY = 500;
private String[] data = {"Mon", "Tues", "Wed", "Thurs", "Fri"};
private JList list = new JList(data);
public Color cellColor = null;
public BlinkingLabelInList() {
add(new JScrollPane(list));
list.setCellRenderer(new MyListCellRenderer());
new Timer(TIMER_DELAY, new TimerListener()).start();
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
cellColor = (cellColor == null) ? FLASH_COLOR : null;
list.repaint();
}
}
private class MyListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component cellRenderer = super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
if (isSelected || cellHasFocus) {
cellRenderer.setForeground(cellColor );
} else {
cellRenderer.setForeground(null);
}
return cellRenderer;
}
}
private static void createAndShowGui() {
BlinkingLabelInList mainPanel = new BlinkingLabelInList();
JFrame frame = new JFrame("BlinkingLabelInList");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}