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);

}

キーリスナーに到達していない可能性があります

于 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 に答える