Nnnnn
Nnnnn

Reputation: 133

DefaultListCellRenderer working fine but could not get ListCellRenderer to work

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)

Upvotes: 0

Views: 1143

Answers (2)

Nnnnn
Nnnnn

Reputation: 133

I just removed getClass().getResource(...) and it worked just fine. I don't know why but it just did. The working code 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("images/in.png");
        setIcon(imageIcon);
        if (value.equals(runm)) {
            Color fg = Color.BLACK;
            setForeground(fg);
        }

        return c;
    }

}

And after this problem solved i have another question, can this image be added to the far right in the JList.

Upvotes: 0

Thomas Fritsch
Thomas Fritsch

Reputation: 10127

You tried to implement the ListCellRenderer interface from scratch by extending from JLabel (instead of extending from DefaultListCellRenderer which in turn extends from JLabel).

Therefore you would need to implement everything what method DefaultListCellRenderer.getListCellRendererComponent does, Most importantly this involves taking the value passed into this method and putting it into a JLabel.setText call.

In short, I recommend you extend your renderer from DefaultListCellRenderer:

public class RCellRenderer extends DefaultListCellRenderer {

    String runm = "";

    public RCellRenderer2(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;
            c.setForeground(fg);
        }

        return c;
    }
}

Upvotes: 2

Related Questions