471,893 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,893 software developers and data experts.

Problem rendering Combo Box with icon near the text


I am having trouble rendering a combo box with icon near the text. Below is a simple code which demonstrates my problem.

import java.awt.Component;
import java.util.HashMap;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

public class IconComboBox extends javax.swing.JDialog {
private JComboBox jComboBox1;
private String[] modelData;
private HashMap<String, Icon> icons = new HashMap<String, Icon>();

/** Creates new form IconComboBox */
public IconComboBox(java.awt.Frame parent, boolean modal) {
super(parent, modal);

ImageIcon img1 = new ImageIcon("1.png");
ImageIcon img2 = new ImageIcon("2.png");

modelData = new String[] { "Item 1", "Item 2", "Item 3", "Item 4" };



private void initComponents()
jComboBox1 = new javax.swing.JComboBox();

getContentPane().setLayout(new java.awt.FlowLayout());

setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(modelData));

jComboBox1.setRenderer(new TypeListCellRenderer(icons));
setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );


public static void main(String args[]) {

try {
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new IconComboBox(new javax.swing.JFrame(), true).setVisible(true);
catch (Exception e) {
// handle exception

private class TypeListCellRenderer extends JLabel implements ListCellRenderer {
HashMap<String, Icon> icons;

public TypeListCellRenderer(HashMap<String, Icon> icons) {

this.icons = icons;

public Component getListCellRendererComponent( JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus ) {
String type = (String) value;
setText( type );
setIcon( icons.get( type ) );
return this;

When i run this program i will see the combo box with only the icon displayed (no text).
Now if will remark the following line:
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
Then the code works just fine.

Did someone knows how to solve this look and feel issue ?
Dec 13 '07 #1
5 3890
11,448 Expert 8TB
Why don't you put JLabels in the combobox model? A label can contain text as
well as icons and display them nicely so you don't have to do anything then.

kind regards,

Dec 13 '07 #2
sir i had tried with Label but it not working can u plz give me solution i.e.code
Dec 13 '07 #3
11,448 Expert 8TB
sir i had tried with Label but it not working can u plz give me solution i.e.code
I wrote 'JLabel' (a Swing component), not 'Label' (an AWT component). Never mix
the two (Swing and AWT).

kind regards,

Dec 13 '07 #4
thank you sir now its working.
Dec 13 '07 #5
11,448 Expert 8TB
thank you sir now its working.
Good, please note that it's always worth the trouble to see if there's a tutorial for
the topic present in Sun's tutorial sections. The API documentation also mentions
the link to such a tutorial section if present.

kind regards,

Dec 13 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by skender | last post: by
14 posts views Thread by Tony LaPaso | last post: by
6 posts views Thread by Ron L | last post: by
9 posts views Thread by Dave | last post: by
2 posts views Thread by swethak | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.