tags:

views:

60

answers:

5

i need to display a image in JComboBox

+1  A: 

Take a look at this example that appears to do what you want.

http://www.java2s.com/Code/Java/Swing-JFC/CustomComboBoxwithImage.htm

What you are looking for is a custom renderer for the JComboBox. A renderer is simply a JComponent, so if you can create a component (JPanel with the necessary items contained), then you can create almost any result that you can think of). You can even override the paint method if using standard JComponents are not enough for you.

Codemwnci
The default renderer for a JComboBox supports Icons.
camickr
+1  A: 

here is a code example of displaying image in JComboBox -

http://www.java2s.com/Code/Java/Swing-JFC/CustomComboBoxwithImage.htm

Also check this below article on usage -

http://www.iam.ubc.ca/guides/javatut99/uiswing/components/combobox.html

Sachin Shanbhag
The default renderer for a JComboBox supports Icons.
camickr
+1  A: 

Just add an Icon to the model instead of a String:

import java.awt.*;
import javax.swing.*;

public class ComboBoxIcon extends JFrame
{
    JComboBox comboBox;

    public ComboBoxIcon()
    {
        Object[] items =
        {
            new ImageIcon("about16.gif"),
            new ImageIcon("add16.gif"),
            new ImageIcon("copy16.gif")
        };
        comboBox = new JComboBox( items );
        getContentPane().add( comboBox, BorderLayout.NORTH );
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxIcon();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }
}
camickr
A: 

@Sachin Shanbhag @Codemwnci That code is not working properly for me.. ny ohter suggesstions..?? thanks...

@camickr I want image along with the String by the side of it. for eg: in skype u can find the following like (image) available (image) not available (image) busy...... like that... can u help me with that....?????

Harish
Reply by adding a comment not by creating a new answer. Also, don't hijack someone elses question. Your requirement is different and therefore the answer will also be different.
camickr
that my friend who posted the question yesterday.. can u help me out with the answer???
Harish
A: 
public class ComboBoxIcon extends JFrame

{ JComboBox comboBox;

public ComboBoxIcon()
{
    /*Object[] items =
    {
        new ImageIcon("images/1.png"),
        new ImageIcon("images/2.jpg"),
        new ImageIcon("images/3.png")
    };*/
    Object item1 =  new ImageIcon("images/1.png");
    Object item2 =  new ImageIcon("images/2.jpg");
    Object item3 =  new ImageIcon("images/3.png");
    /*Object[] items1 = {"ready","notready"};*/
    //comboBox = new JComboBox( items );
    comboBox = new JComboBox();
    comboBox.addItem(item1 + "Ready");
    comboBox.addItem(item2 + "Not Ready");
    comboBox.addItem(item3 + "available");
    getContentPane().add( comboBox, BorderLayout.NORTH );
}

public static void main(String[] args)
{
    JFrame frame = new ComboBoxIcon();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setLocationRelativeTo( null );
    frame.setVisible( true );
}

} I modified the above code given by camickr, but am not getting the requirement as i mentioned. Its not displaying the image, its displaying only the path of it!!!! ny suggestions????

Harish
You where asked to create your own question!!!!!!!! Your question is different than the question asked here and it gets confusing when you have to different answers to two different questions in the same thread. Learn how to use the forums properly!
camickr