469,268 Members | 1,025 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Problem rendering Combo Box with icon near the text

Hi

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" };

icons.put(modelData[0],img1);
icons.put(modelData[1],img1);
icons.put(modelData[2],img2);
icons.put(modelData[3],img2);

initComponents();
}


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));
getContentPane().add(jComboBox1);

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

pack();
}

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) {
super();

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 3795
JosAH
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,

Jos
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
JosAH
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,

Jos
Dec 13 '07 #4
thank you sir now its working.
Dec 13 '07 #5
JosAH
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,

Jos
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.