简体   繁体   English

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

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

I'm using a String array to populate a combo box with items. 我正在使用String数组填充带有项目的组合框。 After an item has been selected and submitted on a button press I want it to remove the item from the combo box. 选择一个项目并将其提交到按钮上后,按一下我希望它从组合框中删除该项目。 My attempt was to remove the selected item from the String array first, remove all items from the combo box and repopulate it with the String array. 我的尝试是首先从String数组中删除选定的项目,从组合框中删除所有项目,然后用String数组重新填充它。

choice is the String array, cboChoice is the combobox, strChoice is the item getting removed 选择是字符串数组,cboChoice是组合框,strChoice是要删除的项目

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

This is as far as I've got, I don't know if there is a simpler method of doing this but I can't seem to get it working. 据我所知,我不知道是否有一种更简单的方法来执行此操作,但是我似乎无法使其正常运行。

If you check the jComboBox Javadoc you will see 如果您检查jComboBox Javadoc,您将看到

removeItem(Object anObject) Removes an item from the item list. removeItem(Object anObject)从项目列表中删除项目。

Just call that to remove the object you no longer want. 只需调用它即可删除不再需要的对象。

The code you've proposed would sort of work (although I'm not sure off hand what the jComboBox will do with the null values) but is not particularly efficient. 您提出的代码可以完成一些工作(尽管我不确定jComboBox将使用null值做什么),但并不是特别有效。

Since you have a String array and a JComboBox that have the same items in the same order, you can use the JComboBox.getSelectedIndex() to retrieve the index location of the selected item and remove from the JComboBox and you're array. 由于您有一个String数组和一个JComboBox,它们具有相同的项,并且顺序相同,因此可以使用JComboBox.getSelectedIndex()检索所选项目的索引位置,并从JComboBox中删除,从而得到数组。

As a suggestion, I would make your String array an ArrayList, it's a "smarter" dynamic array and can stay better in synch with your JComboBox. 作为建议,我将您的String数组设置为ArrayList,这是一个“更智能”的动态数组,并且可以与JComboBox保持更好的同步。 Also make sure you remove from your array first, before removing from your JComboBox, otherwise the selected index could change. 另外,还要确保先从数组中删除,然后再从JComboBox中删除,否则所选索引可能会更改。

An ArrayList declaration would look like this: ArrayList声明如下所示:

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

Add your content to this List like so: 像这样将您的内容添加到此列表中:

choice.add(yourChoice);

Removing the items would be as followed: 删除项目如下:

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

Hope this helps... Also, once you understand how this works, I would suggest studying the ComboBoxModel. 希望对您有所帮助。此外,一旦您了解了它的工作原理,我建议您学习ComboBoxModel。 Certain swing controls have model objects that you can use to add/remove/modify contents without having to reference the actual control. 某些挥杆控件具有模型对象,您可以在无需参考实际控件的情况下添加/删除/修改内容。

This code works, but there is still one issue; 这段代码有效,但是仍然存在一个问题。 you can't remove the last item in the list. 您无法删除列表中的最后一项。 To solve this you could add an element to the list that you just ignore in the remove step. 为了解决这个问题,您可以在删除步骤中将一个元素添加到您只是忽略的列表中。 I have used a "" at the beginning of the list in past. 过去,我在列表的开头使用了“”。

Also I would point out that most examples of JComboBox show using strings, but you can put any type of Object you want in the box. 我还要指出,JComboBox的大多数示例都是使用字符串显示的,但是您可以将所需的任何类型的对象放入框中。 The item in the box will show Object.toString(). 框中的项目将显示Object.toString()。 In many cases it is more useful and straight forward to get back the instance you want than to have to look it up in a list based on information taken from the ComboBox. 在许多情况下,取回想要的实例比直接根据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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Java的组合框中删除Ljava.lang.String? - How to remove Ljava.lang.String from a combo box in java? 如何从组合框中的列表中删除 - how to remove from a list from a combo box 从带有Java的Selenium Webdriver的“组合”框中选择一个项目 - Choose an item from Combo box in Selenium Webdriver with Java 使用 java 从组合框 selenium 驱动程序中选择一个项目 - Selecting an item from a combo box selenium driver with java 如何在Java中实现一个组合框,该组合框将为所选项目输出一定数量的数字? - How to implement a combo box in Java that will output a certain number for the item selected? 如何将数组值添加到第二个组合框模型项中,从第一个组合框所选项目中获取数组名称? - How can I add array value into 2nd Combo Box model item getting the array name from 1st Combo Box selected item? Java组合框模型并获取所选项目 - Java combo box model and get selected item 我如何在向组合框添加项目时停止触发swing JCombo Box项目侦听器。 - How i stop triggering swing JCombo Box item listener while adding item to combo box. 如何从组合框 1 中跟踪选定的选项并在 java 的组合框 2 上显示相应的选项 - How to track selected option from Combo Box 1 and show corresponding option on combo box 2 in java 如何从 Java swing 中的 txt 文件中将特定数据放入组合框中 - How do i get the particular data into the combo box from txt file in Java swing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM