Oct 31

You can achieve that by creating a custom ListCellRenderer. Easiest is to subclass DefaultListCellRenderer and override the getListCellRendererComponent() method to add the appropriate icon.

That just leaves how to determine which icon to use for a given combo item value. One solution to this is to provide the renderer with a set of mappings containing what icon to use for which value.


import java.awt.Component;
import java.util.HashMap;
import java.util.Map;

import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.plaf.metal.MetalIconFactory;


public class IconListRenderer 
	extends DefaultListCellRenderer {

	private Map<Object, Icon> icons = null;
	
	public IconListRenderer(Map<Object, Icon> 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;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// setup mappings for which icon to use for each value
		
		Map<Object, Icon> icons = new HashMap<Object, Icon>();
		icons.put("details", 
			MetalIconFactory.getFileChooserDetailViewIcon());
		icons.put("folder", 
			MetalIconFactory.getTreeFolderIcon());
		icons.put("computer", 
			MetalIconFactory.getTreeComputerIcon());
		
		JFrame frame = new JFrame("Icon List");
		frame.setDefaultCloseOperation(
			JFrame.DISPOSE_ON_CLOSE);
		
		// create a list with some test data
		
		JComboBox combo = new JComboBox(
			new Object[] { 
				"details", "computer", "folder", "computer"});
		
		// create a cell renderer to add the appropriate icon
		
		combo.setRenderer(new IconListRenderer(icons));
		frame.add(combo);
		frame.pack();
		frame.setVisible(true);
	}

}

written by objects \\ tags: , ,


3 Responses to “How do I display a JComboBox with icons and text?”

  1. KOKORA Says:

    good

  2. peter Says:

    I sit with Java in two months and every time I start to understand anything, something like this blows my mind. I sit all day long and I don’t get it. Even if I can make it work. Thanks for tips.

  3. StaticRich Says:

    AWESOME! Such a handy fix and can be used in many other ways too! Took quite a bit of digging around to find this, unfortunately…

Leave a Reply

You must be logged in to post a comment.