i need to display a image in JComboBox
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.
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
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 );
}
}
@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....?????
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????