Remove element from JComboBox in Java
Description
The following code shows how to remove element from JComboBox.
Example
import java.awt.BorderLayout;
/* w w w.j av a2s. c o m*/
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
String labels[] = { "A", "B", "C", "D", "E", "F" };
JFrame frame = new JFrame("Selecting JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox comboBox = new JComboBox(labels);
frame.add(comboBox, BorderLayout.SOUTH);
comboBox.removeItemAt(0);
//Remove the last item
//cb.removeItemAt(cb.getItemCount() - 1);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »