JComboBox.remove不起作用

时间:2013-04-09 13:21:22

标签: java swing jcombobox

我正在尝试删除JComboBox中的所选项目(我已经添加了设计时间)但是不执行删除功能。我做错了什么

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

3 个答案:

答案 0 :(得分:4)

remove中没有此类方法Class JComboBox

您想使用public void removeItemAt(int anIndex)

  

删除anIndex处的项目此方法仅适用于JComboBox   使用可变数据模型。

jComboBox1.removeItemAt(jComboBox1.getSelectedIndex());

答案 1 :(得分:1)

来自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

但如上面的答案所述,你需要做

jComboBox1.removeItemAt(jComboBox1.getSelectedIndex());

希望有所帮助

答案 2 :(得分:0)

看起来你想要jComboBox1.removeItemAt(...)

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