Credits

Saturday, February 13, 2010

Java Combo Box

Another GUI component of Java is a combo box. This is a short drop - down list that allows a user to select an option.

To create this component, use the JComboBox class. Check the sample program below. 

[+/-] show/hide




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

public class ComboBoxes extends JFrame {
public ComboBoxes() {
super("Combo Boxes");
setSize(345, 120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComboBox profession = new JComboBox();
FlowLayout flow = new FlowLayout();
profession.addItem("Programmer");
profession.addItem("Analyst");
profession.addItem("Programmer");
profession.addItem("Database Administrator");
profession.addItem("Network Administrator");
profession.addItem("Project Manager");
setLayout(flow);
add(profession);
setVisible(true);
}

public static void main(String[] arguments) {
ComboBoxes app = new ComboBoxes();
}
}

AddThis