Java Programming Tutorial - 69 - Drop Down List Program

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

public class Gui extends JFrame {
 private JComboBox box;
 private JLabel picture;
 private static String[] filename = {"b.png", "x.png"};
 private Icon[] pics = {new ImageIcon (getClass().getResource(filename[0]))};

 public Gui() {
super ("the title");
setLayout (new FlowLayout());
 
box = new JComboBox(filename)
 
box.addItemListener(
new ItemListener() {
 
public void itemStateChanged (ItemEvent event) {
if(event.getStateChange()== ItemEvent.SELECTED)
picture.setIcon(pics[box.getSelectedIndex()]);
}
}
);
add(box); 
picture = new JLabel (pics[0]);
add(picture);

}
}

package apples;
import javax.swing.JFrame;
public class apples  {
public static void main (String[] args) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
go.setSize(300, 200);
go.setVisible(true);
 }

Comments