Hallo,
ich habe folgenden Code eines eigenen ListCellRenderers mit dessen Hilfe ich durch
„((ListItem)panel.jList1Model.getElementAt(position)).setColor(Color.GREEN);“
einzelne Zeilen der Liste einfärbe.
Allerdings bekomme ich in sehr unregelmäßigen Abständen eine Exception…umso öfter ich mit verschiedenen Timern Farben ändere umso öfter erscheint der Fehler aber warum ist mir rätselhaft :-/
Exception in thread „AWT-EventQueue-0“ java.lang.NullPointerException
at javax.swing.plaf.basic.BasicListUI.getHeight(Unknown Source)
at javax.swing.plaf.basic.BasicListUI.paintImpl(Unknown Source)
at javax.swing.plaf.basic.BasicListUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
CODE:
package Notifire;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class ListItem {
private Color color;
private String value;
public ListItem(Color c, String s) {
color = c;
value = s;
}
public Color getColor() {
return color;
}
public void setColor(Color c) {
color = c;
}
public String getValue() {
return value;
}
}
class CustomCellRenderer extends JLabel implements ListCellRenderer {
public CustomCellRenderer() {
// Don't paint behind the component
setOpaque(true);
}
// Set the attributes of the
// class and return a reference
public Component getListCellRendererComponent(JList list, Object value, // value
// to
// display
int index, // cell index
boolean iss, // is selected
boolean chf) // cell has focus?
{
// Set the text and color
// background for rendering
try {
setText(((ListItem) value).getValue());
setBackground(((ListItem) value).getColor());
// Set a border if the list
// item is selected
if (iss) {
setBorder(BorderFactory.createLineBorder(Color.blue, 2));
} else {
setBorder(BorderFactory.createLineBorder(list.getBackground(),
2));
}
return this;
} catch (Exception e) {
return this;
}
}
}