0

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)
Nnnnn
  • 133
  • 3
  • 15
  • You really should extend DefaultListCellRenderer instead of implementing the interface directly. Read [the documentation](https://docs.oracle.com/javase/10/docs/api/javax/swing/DefaultListCellRenderer.html) to understand why. Check that getResource("/images/im.png") actually returns a non-null value. And obviously, your renderer needs to call setIcon (which it does not do in the code you’ve posted). – VGR Aug 10 '18 at 14:03
  • @VGR i have tried doing that and added everything in my code and i tried using DefaultListCellRenderer it gives a null pointer exception at ImageIcon – Nnnnn Aug 10 '18 at 14:06
  • If your ImageIcon construction throws a NullPointerException, then how are you expecting an icon to be displayed? If you’re running from a .jar file, check that it has an images/im.png entry. If you are not running from a .jar, check that the parent directory of images is in your runtime classpath. – VGR Aug 10 '18 at 14:09
  • The directory is all fine i double checked is there any other reason i may be getting that. Or should i update the code in the question with the DefaultListCellRenderer code for better view. – Nnnnn Aug 10 '18 at 14:18
  • 1
    There is no point in examining your renderer until the ImageIcon code completes without any exceptions. – VGR Aug 10 '18 at 14:20
  • @VGR but you can always point me in the right direction where should i look for do that – Nnnnn Aug 10 '18 at 14:43
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Aug 10 '18 at 15:45

2 Answers2

2

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;
    }
}
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
  • i tried exactly this this works like charm it should but the problem is when i add the code of ImageIcon as mentioned in the question it gives me null pointer exception i don't know why. – Nnnnn Aug 10 '18 at 14:11
  • @Nnnnn You should post your exception stack trace (add it into your question). May be this exception has nothing to do with your renderer code. May be the exception is just thrown by `new ImageIcon(...)`. – Thomas Fritsch Aug 10 '18 at 14:18
0

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.

Nnnnn
  • 133
  • 3
  • 15
  • 1
    *`new ImageIcon("images/in.png");`* That won't work when the app. is deployed (though it will fail without throwing an exception) & should be done in the constructor in any case. – Andrew Thompson Aug 10 '18 at 15:46
  • Can you help me understand why it will fail because in my app I am using several resources from outside like files, images and what not, can you please point me to the right article to get this. – Nnnnn Aug 10 '18 at 15:57
  • Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Aug 10 '18 at 16:07