• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding images to a JPanel based on JComboBox selection.  RSS feed

 
Caitlin Gibson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
In the following code:

I would like to display the image that corresponds to the student name that was selected
from the JComboBox in a panel. If you could show me how to do this with a small code sample that would be great. The other problem would be displaying some personal info about this student *below* their image.
Thanks,
-CaitlinG.
(edited by Cindy to format code using [ code][ /code] tags).
[ November 27, 2002: Message edited by: Cindy Glass ]
 
Bhagya Tangutur
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i guess add a JLabel to Panel and setting image to this label must work fine...Hope this is how u want the functionality...
lets' say

JPanel panel=new JPanel();
JLabel lbl=new JLabel();
panel.add(lbl);

JComboBox combo=new JComboBox();
combo.addItem(new Person(args));
combo.addItem(new Person(args));
combo.addItem(new Person(args));
combo.addItem(new Person(args));
combo.additemListener(new ItemListener(){
public void itemStatechanged(ItemEvent e){
lbl.setIcon(((Person)combo.getSelectedItem()).getIconImage());
}});

class Person {
public Person(args){
}

public ImageIcon getImage(){
return someImage...;
}
}
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!