JComboBox.removeItem(Object object)
Once you addItems to JComboBox,you should also learn how to remove the same objects using JComboBox.removeItem(Object object) method.
JComboBox.removeItem(Object object) Example
[
import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JComboBox; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; public class JComboBoxRemoveItems extends JFrame implements ActionListener{ public JButton b; public JComboBox box; public JComboBoxRemoveItems() { super("Remove JComboBox Items"); String names []={"Eric","Mutua","James","Bill","Obama"}; box=new JComboBox(names); box.setBounds(50,50,150,30); add(box); b=new JButton("Remove"); b.setBounds(50, 100, 150, 30); add(b); b.addActionListener(this); setSize(400,400); setLayout(null); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent event) { Object item =box.getSelectedItem(); box.removeItem(item); String s=(String)item; JOptionPane.showMessageDialog(this,"Item remove is "+s, "Removing Item from JcomboBox",JOptionPane.INFORMATION_MESSAGE); } public static void main(String args[]) { new JComboBoxRemoveItems(); } }
]
Tips to learn from above program code
I have introduced some java.swing components in this program.These are:-
Above class extends JFrame and implements ActionListener interface.
Note:
If you implement a certain interface you are required to override Interface abstract method i.e
[
public void actionPerformed(ActionEvent event)
{
Object item =box.getSelectedItem();
box.removeItem(item);
String s=(String)item;
JOptionPane.showMessageDialog(this,"Item remove is "+s,
"Removing Item from JcomboBox",JOptionPane.INFORMATION_MESSAGE);
}
]
See also:
public void actionPerformed(ActionEvent event) { Object item =box.getSelectedItem(); box.removeItem(item); String s=(String)item; JOptionPane.showMessageDialog(this,"Item remove is "+s, "Removing Item from JcomboBox",JOptionPane.INFORMATION_MESSAGE); }