Problem rendering Combo Box with icon near the text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wajedali
    New Member
    • Dec 2007
    • 7

    #1

    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.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 ?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • wajedali
      New Member
      • Dec 2007
      • 7

      #3
      sir i had tried with Label but it not working can u plz give me solution i.e.code

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by wajedali
        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

        Comment

        • wajedali
          New Member
          • Dec 2007
          • 7

          #5
          thank you sir now its working.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by wajedali
            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

            Comment

            Working...