Java Program to remove items in JComboBox by user input


In this program, you will learn how to remove items in JComboBox by user input in Java.


JComboBox cb;

cb.removeItem(t1.getText());


Example: How to remove items in JComboBox by user input in Java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Test implements ActionListener {

	JButton jb1;
	JComboBox cb;

	JTextField t1;
	JFrame f;

	Test() {

		f = new JFrame("ComboBox Example by Xiith");
		String country[] = { "India", "Aus", "China", "England" };

		cb = new JComboBox(country);
		cb.setBounds(50, 30, 150, 30);
		f.add(cb);

		t1 = new JTextField();
		t1.setBounds(60, 100, 120, 30);
		f.add(t1);

		jb1 = new JButton("Remove Item");
		jb1.setBounds(50, 200, 180, 30);
		f.add(jb1);

		jb1.addActionListener(this);

		f.setLayout(null);
		f.setSize(600, 500);
		f.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		cb.removeItem(t1.getText());
	}

	public static void main(String args[]) {
		Test t = new Test();
	}
}

Output:

JComboBox-remove-items

JComboBox-remove-items