Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6222255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:16:57+00:00 2026-05-24T08:16:57+00:00

i have a simple example of a JList that is getting it’s data from

  • 0

i have a simple example of a JList that is getting it’s data from an ArrayList but i want to show an image next to each string in the list. I have written a custom cell renderer (IconListRenderer) that is suppose to display an icon and the object side-to-side.

Here is a running sample.

//Test class showing the list in a frame
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.LineBorder;


public class Test extends JFrame{

     public static void main(String[] args) {


    final JFileChooser chooser = new JFileChooser();
    JButton button = new JButton();
    button.setText("Upload");
    JFrame frame = new JFrame("My Frame");
    JList list = new JList();
    Map<Object, ImageIcon> icons = new HashMap<Object, ImageIcon>();
    list.setBorder(new LineBorder(Color.BLACK));

    ImageIcon icon = new ImageIcon("/Images/400px-Greek_uc_sigma.png");

    ArrayList<String> arrayList = new ArrayList<String>();

    icons.put("Name", icon);

    //populate the arrayList for testing
    arrayList.add("Smith");
    arrayList.add("John");
    arrayList.add("Bob");
    arrayList.add("Kim");

    frame.setSize(new Dimension(400, 400));
    //set the list data
    list.setListData(arrayList.toArray());
    final JFrame imageFrame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    list.setCellRenderer(new IconListRenderer(icons));
    frame.add(list); 
    frame.setVisible(true);
    frame.repaint();
}



}

//IconListRenderer class

import java.awt.Component;
import java.util.Map;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;


 public class IconListRenderer
                           extends DefaultListCellRenderer {

private Map<Object, ImageIcon> icons = null;

public IconListRenderer(Map<Object, ImageIcon> icons) {
    this.icons = icons;
}

@Override
public Component getListCellRendererComponent(
    JList list, Object value, int index,
    boolean isSelected, boolean cellHasFocus) {

    // Get the renderer component from parent class

    JLabel label =
        (JLabel) super.getListCellRendererComponent(list,
            value, index, isSelected, cellHasFocus);

    // Get icon to use for the list item value

    Icon icon = icons.get(value);

    // Set icon to display for value

    label.setIcon(icon);
    return label;
}
  }

The list shows currently but no image?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:16:57+00:00Added an answer on May 24, 2026 at 8:16 am

    It’s not really surprising:

    • you use the JList item value as a key to get the icon from the icon map;
    • the icon map contains a single key: “Name”
    • the JList contains “Smith”, “John”, “Bob” and “Kim”, but no “Name”
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some simple shell scripting tasks that I want to do For example:
Does someone have a simple example of threading in c? I want to build
I have a simple example below that does not compile. i get the following
i want the most simple example from scratch how to open new db populate
Can I have a simple example of an assembly code that when compiled into
I have a simple form and want to verify that the posted value came
I have a simple script named example : #!/bin/sh echo $'${1}' Please note that
I have an extremely simple example trying to use Data Annotations with unobtrusive javascript
I have seen simple example Ajax source codes in many online tutorials. What I
Below I have a very simple example of what I'm trying to do. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.