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"));
}
}

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

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();
         }
        }

Edited 3 Years Ago by Nick Evan: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.