I need to remove all items from the combo box
int itemCount = combo.getItemCount(); for(int i=0;i<itemCount;i++){ combo.removeItemAt(0); }
This code will delete all items except the last. This gives a NullPointerException. How to fix it?
The code in the question usually works. However, this seems like a threading issue. Another thread can ruin the elements.
However, I want you to use the method better removeAllItems();:
removeAllItems();
combo.removeAllItems();
What about JComboBox.removeAllItems () ?
:
combo.removeItemAt(0);
combo.removeItemAt(0)
, 0 i.
0
i
for(int i=combo.getItemCount()-1;i>=0;i--){ combo.removeItemAt(i); }
combo.removeAllItems()
.removeAllItems() .
, , . , .
- , , - , .
, - ( actionPeformed()) combo.removeItemAt(0) removeAllItems(), actionPerformed /. actionPerformed() ( - ). , , , actionPerformed() .
, actionPerformed(), . mouseClicked() , .
removeAllItems () it removes everything, but after adding data to the combo box it will not be displayed there, a nullPointException will show
Use this to remove all items from a combo box:
DefaultComboBoxModel model = (DefaultComboBoxModel) ComboBox.getModel(); model.removeAllElements();
This usually happens because you have an event related to JComboBox. It is resolved if you have a control in JComboBox, for example:
jComboBoxExample.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { do_run (); } }); public void do_run() { int n=jComboBoxPerfilDocumentos.getItemCount(); <--THIS IS THE SOLUTION if (n> 0) { String x = jComboBoxPerfilDocumentos.getSelectedItem (). ToString (); } }
You can use
this.combo.removeAllItems ();
delete all items in JComboBox.