3

这是我的第一篇文章,我认为我做得对。

我有一个程序,它从 AutoComplete jComboBox 获取用户输入,然后将输入发送到文本文件中。(AutoComplete 使用库 glazedlists_java15/1.8.0 完成)。

使用自动完成功能后,我必须将 jComboBox 设置为 DefaultComboBoxModel。

当用户按下Enter 键时,jComboBox 应该使用从键盘输入的新项目更新列表,因此用户可以看到jComboBox 列表中最后输入的项目。

是通过从 jComboBox 中删除所有项目然后再次插入它们来完成的。

问题是,拥有自动完成功能之前,我只能说jComboBox1.removeAllItems(); 但现在由于模型的原因,我必须使用 model.removeAllElements();

public class Test {
    final static DefaultComboBoxModel model = new DefaultComboBoxModel();
    static JComboBox c                = new JComboBox(model);
    private static final long serialVersionUID = 1L;
    private static JButton b = new JButton();
    static JFrame f = new JFrame();
    /**
     * @param args
     */
    public static void TestFrame() {
        String[] a = {"hi1" , "hi2", "hi3", "hi4","hi5"};
        AutoCompleteSupport support = AutoCompleteSupport.install(c,
                GlazedLists.eventListOf(a));
        JPanel test = new JPanel();
        test.add(b);
        test.add(c);
        model.addElement(a);
        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                model.removeAllElements();

            }



        });
        f.add(test);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setSize(500,500);

    }

问题是model.removeAllElements(); model.addElement(s);不工作,所以我不能更新 jComboBox。你能不能花点时间帮我找到解决方案。谢谢!

4

2 回答 2

2

编辑:

我不知道你的问题出在哪里,这完全适合我

final DefaultComboBoxModel model = new DefaultComboBoxModel();
JComboBox c                = new JComboBox(model);
private static final long serialVersionUID = 1L;
private JButton b = new JButton();

public TestFrame() {
    JPanel test = new JPanel();
    test.add(b);
    test.add(c);
    model.addElement("hi");

    b.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            model.removeAllElements();

        }
    });
    this.add(test);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setSize(500,500);

}

也许你没有联系到你的 keylistener

于 2012-11-21T14:56:53.727 回答
0
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.AutoCompleteSupport;

public class TestFrame
{
    private static JComboBox c = new JComboBox();
    private static JButton b = new JButton();
    private static JFrame f = new JFrame();
    private static String[] a = {"hi1", "hi2", "hi3", "hi4", "hi5"};

    public static void TestFrame()
    {
        final EventList<String> items = GlazedLists.eventListOf(a);
        AutoCompleteSupport.install(c, items);
        JPanel test = new JPanel();
        test.add(b);
        test.add(c);
        c.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                c = (JComboBox) e.getSource();

                if (e.getActionCommand().equals("comboBoxEdited"))
                {
                    items.clear();
                }
            }
        });
        b.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                items.clear();
            }
        });
        f.add(test);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setSize(500, 500);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                TestFrame();
            }
        });
    }
}
于 2012-11-29T12:41:54.843 回答