JCombobox 无法将多个选项重命名为相同的值

JCombobox can't rename multiple options to the same value

我正在尝试将多个选项重命名为相同的值。如果已经有一个选项具有我正在尝试重命名另一个的值,则没有任何反应。

这是我当前的代码:

package main.cache;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ComboBox {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ComboBox window = new ComboBox();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ComboBox() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        String[] values = new String[] { "null", "null", "parameter3" };

        final DefaultComboBoxModel<Object> models = new DefaultComboBoxModel<Object>(values);
        JComboBox<Object> comboBox = new JComboBox<Object>(models);
        comboBox.setBounds(130, 82, 168, 40);
        frame.getContentPane().add(comboBox);

        comboBox.setEditable(true);
        comboBox.addActionListener(new ActionListener() {
            private int localSelectedIndex = -1;

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = comboBox.getSelectedIndex();
                if (index >= 0) {
                    localSelectedIndex = index;
                } else if ("comboBoxEdited".equals(e.getActionCommand())) {
                    String newValue = (String) models.getSelectedItem();

                    // Change the value of the selected option
                    Object[] objects = new String[models.getSize()];
                    for (int i = 0; i < objects.length; i++) {
                        if (localSelectedIndex == i) {
                            objects[i] = newValue;
                        } else {
                            objects[i] = models.getElementAt(i);
                        }
                    }

                    // remove the elements and re add them
                    models.removeAllElements();
                    for (int i = 0; i < objects.length; i++) {
                        models.addElement(objects[i]);
                    }

                    // re-select the edited item
                    comboBox.setSelectedItem(newValue);
                }
            }
        });

    }
}

例如在这里,我有3个选项"null"、"null"、"parameter3"。假设我也想将第二个选项编辑为 parameter3。目前,没有任何反应。

出现此问题是因为 JComboBox 选择了第一次出现的字符串。因此,唯一的解决方案是自定义 class 说称为 Element 并覆盖 toString,然后添加元素而不是将原始字符串添加到 JComboBox。