如何从 Java 的组合框中删除特定项目?

How would I remove a specific item from a combo box in Java?

我正在使用字符串数组来填充带有项目的组合框。在按下按钮选择并提交项目后,我希望它从组合框中删除该项目。我的尝试是先从字符串数组中删除所选项目,从组合框中删除所有项目,然后用字符串数组重新填充它。

choice 是字符串数组,cboChoice 是组合框,strChoice 是要删除的项目

for(int i = 0; i < choice.length; i++) {
        if(choice[i].equals(strChoice)) {
            choice[i] = null;
            cboChoice.removeAllItems();
            cboChoice.addItem(choice);
        }
    }

就我所知,我不知道是否有更简单的方法,但我似乎无法让它工作。

如果你检查 jComboBox Javadoc 你会看到

removeItem(Object anObject) Removes an item from the item list.

只需调用它即可删除您不再需要的对象。

您提出的代码可以工作(虽然我不确定 jComboBox 将如何处理空值)但不是特别有效。

由于您有一个字符串数组和一个 JComboBox,它们具有相同顺序的相同项目,您可以使用 JComboBox.getSelectedIndex() 检索所选项目的索引位置并从 JComboBox 中删除,然后您'重新排列。

作为一个建议,我会让您的字符串数组成为一个 ArrayList,它是一个 "smarter" 动态数组并且可以更好地与您的 JComboBox 保持同步。还要确保先从数组中删除,然后再从 JComboBox 中删除,否则所选索引可能会更改。

ArrayList 声明如下所示:

ArrayList<String> choice = new ArrayList<>();

像这样将您的内容添加到此列表:

choice.add(yourChoice);

删除项目如下:

if (cboChoice.getSelectedIndex() > -1) {
        choice.remove(cboChoice.getSelectedIndex());
        cboChoice.getSelectedIndex();
}

希望这对您有所帮助...此外,一旦您理解了它的工作原理,我建议您研究一下 ComboBoxModel。某些 Swing 控件具有模型对象,您可以将其用于 add/remove/modify 内容,而无需引用实际控件。

此代码有效,但仍有一个问题;您无法删除列表中的最后一项。要解决此问题,您可以将一个元素添加到您在删除步骤中忽略的列表中。我过去在列表的开头使用了一个“”。

我还要指出,大多数 JComboBox 示例都使用字符串显示,但您可以将任何类型的对象放入框中。框中的项目将显示 Object.toString()。在许多情况下,取回您想要的实例比必须根据从 ComboBox 获取的信息在列表中查找它更有用和直接。

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by bday on 4/22/15.<br>
 * <br>
 *  ItemRemovingComboBox will do something useful I'm sure
 */
public class ItemRemovingComboBox {
    private final JFrame frame = new JFrame();
    private final List<String> strings = new ArrayList<String>();
    private final JComboBox cb;
    private final ItemListener itemListener;

    public ItemRemovingComboBox()
    {
        String[] testItems = new String[] {"one", "two", "three"};
        strings.addAll(Arrays.asList(testItems));
        cb = new JComboBox(testItems);
        frame.add(cb);
        frame.setSize(200, 200);
        frame.setVisible(true);

        itemListener = new ItemListener() {
            public void itemStateChanged(ItemEvent itemEvent) {
                if (itemEvent.getStateChange() == ItemEvent.SELECTED) {
                    String item = (String) itemEvent.getItem();
                    System.out.println("Item: " + item + " removed from list");
                    removeItem(item);
                }
            }
        };
        cb.addItemListener(itemListener);
    }

    private void removeItem(String item) {
        //this step is required to keep from calling back to the listener with new selection when item is removed
        cb.removeItemListener(itemListener);
        strings.remove(item);
        cb.removeItem(item);
        cb.addItemListener(itemListener); //okay now we what to know about changes again
    }

    public static void main(String[] args) {
        new ItemRemovingComboBox();
    }
}