Combo1.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
JComboBox demo.
*/
public class Combo1 extends JFrame
{
public static void main(String[] argv)
{
Combo1 c = new Combo1();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.show();
}
public Combo1()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension ss = kit.getScreenSize();
setSize(ss.width/2,ss.height/6);
setLocation(ss.width/4,ss.height/4);
setTitle("CISC370-011 -- JComboBox demo");
Container cp = getContentPane();
JPanel p = new JPanel();
cp.add(p);
JLabel label = new JLabel("Pick your favorite dwarf:");
cp.add(label,BorderLayout.NORTH);
String[] labels = {
"Happy",
"Sleepy",
"Dopey",
"Grumpy",
"Sneezy",
"Doc",
"Bashful",
};
JComboBox box = new JComboBox();
for (int i=0; i<labels.length; i++)
box.addItem(labels[i]);
box.addActionListener(new Boxer(box));
p.add(box);
}
}
/**
An ActionListener to watch for changes in a JComboBox.
*/
class Boxer implements ActionListener
{
public Boxer(JComboBox box)
{ this.box = box; }
public void actionPerformed(ActionEvent e)
{
System.out.println("Selected " + box.getSelectedItem());
}
private JComboBox box;
}