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.Compon ent;
import java.util.HashM ap;
import javax.swing.Ico n;
import javax.swing.Ima geIcon;
import javax.swing.JCo mboBox;
import javax.swing.JLa bel;
import javax.swing.JLi st;
import javax.swing.Lis tCellRenderer;
import javax.swing.UIM anager;
import javax.swing.Win dowConstants;
public class IconComboBox extends javax.swing.JDi alog {
private JComboBox jComboBox1;
private String[] modelData;
private HashMap<String, Icon> icons = new HashMap<String, Icon>();
/** Creates new form IconComboBox */
public IconComboBox(ja va.awt.Frame parent, boolean modal) {
super(parent, modal);
ImageIcon img1 = new ImageIcon("1.pn g");
ImageIcon img2 = new ImageIcon("2.pn g");
modelData = new String[] { "Item 1", "Item 2", "Item 3", "Item 4" };
icons.put(model Data[0],img1);
icons.put(model Data[1],img1);
icons.put(model Data[2],img2);
icons.put(model Data[3],img2);
initComponents( );
}
private void initComponents( )
{
jComboBox1 = new javax.swing.JCo mboBox();
getContentPane( ).setLayout(new java.awt.FlowLa yout());
setDefaultClose Operation(javax .swing.WindowCo nstants.DISPOSE _ON_CLOSE);
jComboBox1.setM odel(new javax.swing.Def aultComboBoxMod el(modelData));
getContentPane( ).add(jComboBox 1);
jComboBox1.setR enderer(new TypeListCellRen derer(icons));
setDefaultClose Operation( WindowConstants .DISPOSE_ON_CLO SE );
pack();
}
public static void main(String args[]) {
try {
UIManager.setLo okAndFeel(UIMan ager.getSystemL ookAndFeelClass Name());
java.awt.EventQ ueue.invokeLate r(new Runnable() {
public void run() {
new IconComboBox(ne w javax.swing.JFr ame(), true).setVisibl e(true);
}
});
}
catch (Exception e) {
// handle exception
}
}
private class TypeListCellRen derer extends JLabel implements ListCellRendere r {
HashMap<String, Icon> icons;
public TypeListCellRen derer(HashMap<S tring, Icon> icons) {
super();
this.icons = icons;
}
public Component getListCellRend ererComponent( 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.setLo okAndFeel(UIMan ager.getSystemL ookAndFeelClass Name());
Then the code works just fine.
Did someone knows how to solve this look and feel issue ?
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.Compon ent;
import java.util.HashM ap;
import javax.swing.Ico n;
import javax.swing.Ima geIcon;
import javax.swing.JCo mboBox;
import javax.swing.JLa bel;
import javax.swing.JLi st;
import javax.swing.Lis tCellRenderer;
import javax.swing.UIM anager;
import javax.swing.Win dowConstants;
public class IconComboBox extends javax.swing.JDi alog {
private JComboBox jComboBox1;
private String[] modelData;
private HashMap<String, Icon> icons = new HashMap<String, Icon>();
/** Creates new form IconComboBox */
public IconComboBox(ja va.awt.Frame parent, boolean modal) {
super(parent, modal);
ImageIcon img1 = new ImageIcon("1.pn g");
ImageIcon img2 = new ImageIcon("2.pn g");
modelData = new String[] { "Item 1", "Item 2", "Item 3", "Item 4" };
icons.put(model Data[0],img1);
icons.put(model Data[1],img1);
icons.put(model Data[2],img2);
icons.put(model Data[3],img2);
initComponents( );
}
private void initComponents( )
{
jComboBox1 = new javax.swing.JCo mboBox();
getContentPane( ).setLayout(new java.awt.FlowLa yout());
setDefaultClose Operation(javax .swing.WindowCo nstants.DISPOSE _ON_CLOSE);
jComboBox1.setM odel(new javax.swing.Def aultComboBoxMod el(modelData));
getContentPane( ).add(jComboBox 1);
jComboBox1.setR enderer(new TypeListCellRen derer(icons));
setDefaultClose Operation( WindowConstants .DISPOSE_ON_CLO SE );
pack();
}
public static void main(String args[]) {
try {
UIManager.setLo okAndFeel(UIMan ager.getSystemL ookAndFeelClass Name());
java.awt.EventQ ueue.invokeLate r(new Runnable() {
public void run() {
new IconComboBox(ne w javax.swing.JFr ame(), true).setVisibl e(true);
}
});
}
catch (Exception e) {
// handle exception
}
}
private class TypeListCellRen derer extends JLabel implements ListCellRendere r {
HashMap<String, Icon> icons;
public TypeListCellRen derer(HashMap<S tring, Icon> icons) {
super();
this.icons = icons;
}
public Component getListCellRend ererComponent( 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.setLo okAndFeel(UIMan ager.getSystemL ookAndFeelClass Name());
Then the code works just fine.
Did someone knows how to solve this look and feel issue ?
Comment