digrev
digrev

Reputation: 97

JComboBox.remove is not working

I am trying to remove selected items in the JComboBox (I have added design time) But remove function is not executed. What am I doing wrong

// jComboBox1.removeAllItems(); working
// jComboBox1.removeItem(jComboBox1.getSelectedItem()); working
jComboBox1.remove(jComboBox1.getSelectedIndex()); not working

Upvotes: 2

Views: 4662

Answers (3)

Jabir
Jabir

Reputation: 2886

From JavaDoc

public void remove(int index)

    Removes the component, specified by index, from this container. This method also notifies the layout manager to remove the component from this container's layout via the removeLayoutComponent method.

    Parameters:
        index - the index of the component to be removed

But as mentioned in above answers you need to do

jComboBox1.removeItemAt(jComboBox1.getSelectedIndex());

Hope it helps

Upvotes: 1

Maroun
Maroun

Reputation: 96018

There is no such method remove in Class JComboBox.

You want to use public void removeItemAt(int anIndex):

Removes the item at anIndex This method works only if the JComboBox uses a mutable data model.

jComboBox1.removeItemAt(jComboBox1.getSelectedIndex());

Upvotes: 6

Vindicare
Vindicare

Reputation: 213

It looks like you want jComboBox1.removeItemAt(...)

see http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html#removeItemAt(int)

Upvotes: 0

Related Questions