hey i select any image from combobox.. the label should updated to display that image.. i tried this code. I am not getting through.,
Just see dis.. please help me... reply fast.. its urgent..

import java.awt.*;
import java.awt.event.ItemEvent;

import javax.swing.*;

public class x extends JFrame
{
JLabel l;
ImageIcon lh0, lh1;

public void init()
{
Container c= getContentPane();
c.setLayout(new FlowLayout());

JComboBox jc= new JComboBox();
jc.addItem("lh0");
jc.addItem("lh1");
c.add(jc);

l= new JLabel (new ImageIcon("lh0.gif"));
c.add(l);
}

public void itemstatechanged(ItemEvent ie)
{
String s=(String)ie.getItem();
l.setIcon(new ImageIcon(s+".gif"));
}
}

Recommended Answers

All 3 Replies

Can't see where you add the listener to the combo box. Is your itemstatechanged method bei ng called?

hey i got the code.. its working.. thnks..
This code is correct na??
but cn u plz tel me any code is there for placing combobox for particular place.???

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

 public class ComboBoxTest extends JFrame
 {
 private JComboBox images;
 private JLabel label;
 private String names[] =
 { "images.jpg", "images10.jpg",
 "images5.jpg", "images7.jpg" };
 private Icon icons[] =
 { new ImageIcon( names[ 0 ] ),
 new ImageIcon( names[ 1 ] ),
 new ImageIcon( names[ 2 ] ),
 new ImageIcon( names[ 3 ] ) };

 public ComboBoxTest()
 {

 Container c = getContentPane();
 c.setLayout( new FlowLayout() );

 images = new JComboBox( names );
 images.setMaximumRowCount( 3 );

 images.addItemListener(
 new ItemListener() {
 public void itemStateChanged( ItemEvent e )
 {
 label.setIcon(
 icons[ images.getSelectedIndex() ] );
 }
 }
 );

 c.add( images );

 label = new JLabel( icons[ 0 ] );
 c.add( label );
 show();
 }

 public static void main( String args[] )
 {
     new ComboBoxTest();
         }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.