Aca Aca - 2 months ago 4x
Java Question

Java Swing countrycombobox with icons

I hope someone can help me out. Iam trying to create a "countrycombobox" with icons in Java Swing. I found some stuff, but nothing did work for me. Maybe the problem is, that Iam still "new" to Java.
I just want it simple like this:
Just the flags in front of the countrys.

I would really appreciate a working example. I really wonder, that there is no standard option or a good code snippet(used Google a lot to find help here) for stuff like this.

Aca Aca

I found a better example and wanna share my stuff with you. There is just one problem left, that I dont get it sized.

package view;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CountryComboBox extends JPanel {
    ImageIcon[] images;
    String[] imgStrings = {"de"};

     * Despite its use of EmptyBorder, this panel makes a fine content
     * pane because the empty border just increases the panel's size
     * and is "painted" on top of the panel's normal background.  In
     * other words, the JPanel fills its entire background if it's
     * opaque (which it is by default); adding a border doesn't change
     * that.
    public CountryComboBox() {
        super(new BorderLayout());

        //Load the images and create an array of indexes.
        images = new ImageIcon[imgStrings.length];
        Integer[] intArray = new Integer[imgStrings.length];
        for (int i = 0; i < imgStrings.length; i++) {
            intArray[i] = new Integer(i);
            images[i] = createImageIcon("/res/" + imgStrings[i] + ".png");
            if (images[i] != null) {

        //Create the combo box.
        JComboBox imgList = new JComboBox(intArray);
        ComboBoxRenderer renderer= new ComboBoxRenderer();

        //Lay out the demo.
        add(imgList, BorderLayout.PAGE_START);

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) { imgURL = CountryComboBox.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
                return null;

    class ComboBoxRenderer extends JLabel
                           implements ListCellRenderer {
        private Font uhOhFont;

        public ComboBoxRenderer() {

         * This method finds the image and text corresponding
         * to the selected value and returns the label, set up
         * to display the text and image.
        public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
            //Get the selected index. (The index param isn't
            //always valid, so just use the value.)
            int selectedIndex = ((Integer)value).intValue();

            if (isSelected) {
            } else {

            //Set the icon and text.  If icon was null, say so.
            ImageIcon icon = images[selectedIndex];
            String img = imgStrings[selectedIndex];
            if (icon != null) {
            } else {
                setUhOhText(img + " (no image available)",

            return this;

        //Set the font and text when no image was found.
        protected void setUhOhText(String uhOhText, Font normalFont) {
            if (uhOhFont == null) { //lazily create this font
                uhOhFont = normalFont.deriveFont(Font.ITALIC);

I call it in a JPanel with absolute layout:

JComponent newContentPane = new CountryComboBox();
newContentPane.setOpaque(true); //content panes must be opaque
newContentPane.setBounds(10, 75, 50, 26);

setBounds isnt working, just to get the right position. I cant size it with this.

Best regards Acanis