I have tried extending DefaultListCellRenderer
for changing the text color and it works fine as it should be.But i could not display the icon in that JList
i am rendering.Then i tried implementing ListCellRenderer
and i am not been able to even display the contents of the JList
. I have set the renderer on the mouse click on the JList
and in case of ListCellRenderer
the list disappears on the mouse click but in case of DefaultListCellRenderer
it works fine.
My first question is why are the contents of the JList
disappearing on the mouse click and second question is why am i not able to add icon by adding following code in case of DefaultListCellRenderer
.
ImageIcon imageIcon = new ImageIcon(getClass().getResource("/images/im.png"));
setIcon(imageIcon);
The following is my whole code for the renderer.
public class RCellRenderer extends JLabel implements ListCellRenderer {
String runm = "";
public RCellRenderer(String runm) {
this.runm = runm;
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
// Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value.equals(runm)) {
Color fg = Color.BLACK;
setForeground(fg);
}
// return c;
return this;
}
}
The code of DefaultListCellRenderer
is as follows:
public class RCellRenderer extends DefaultListCellRenderer {
String runm = "";
public RCellRenderer(String runm) {
this.runm = runm;
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
ImageIcon imageIcon = new ImageIcon(getClass().getResource("images/in.png"));
setIcon(imageIcon);
if (value.equals(runm)) {
Color fg = Color.BLACK;
setForeground(fg);
}
return c;
}
}
And the stack trace upon executing this in my program is as follows:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at services.RCellRenderer.getListCellRendererComponent(RCellRenderer.java:29)
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1361)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
at javax.swing.plaf.basic.BasicListUI.getCellBounds(BasicListUI.java:952)
at javax.swing.plaf.basic.BasicListUI$Handler.repaintCellFocus(BasicListUI.java:2807)
at javax.swing.plaf.basic.BasicListUI$Handler.focusLost(BasicListUI.java:2823)
at java.awt.Component.processFocusEvent(Component.java:6425)
at java.awt.Component.processEvent(Component.java:6289)
at java.awt.Container.processEvent(Container.java:2237)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:1024)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:690)
at java.awt.Component.dispatchEventImpl(Component.java:4760)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4711)
at sun.awt.SunToolkit$1.run(SunToolkit.java:518)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)