使用glazedlists更新/刷新组合框

时间:2014-01-24 02:28:23

标签: java combobox auto-update glazedlists

我怎么可能啊......“自动更新”我的comboBox ..?我正在使用glazedlists autoComplete而且我有点迷失了怎么办...阅读有些像使用eventlists和{{ 1}}但我无法理解如何使其工作.. 请帮忙:( 继承我的示例代码..但我不知道它旁边是什么...尝试使用事件列表,但无法让它自己更新..

basiclist

1 个答案:

答案 0 :(得分:0)

您需要将组合框与事件列表相关联,然后将元素添加到事件列表中。

public class ComboBoxTest {
    private final EventList<String> values;

    public ComboBoxTest() {
        this.values = GlazedLists.eventListOf("A", "B", "C", "D");
    }

    public Component createControl() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(this.createComboBox(), BorderLayout.NORTH);
        panel.add(new JButton(new AbstractAction("Add Elements") {
            @Override
            public void actionPerformed(ActionEvent e) {
                ComboBoxTest.this.addElements();
            }
        }), BorderLayout.SOUTH);

        return panel;
    }

    public void addElements() {
        List<String> toAdd = new ArrayList<>(2);
        toAdd.add("E");
        toAdd.add("F");
        this.values.addAll(toAdd);
    }

    private Component createComboBox() {
        JComboBox<String> box = new JComboBox<>();
        box.setEditable(true);

        AutoCompleteSupport.install(box, this.values);

        return box;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ComboBoxTest testApp = new ComboBoxTest();
                JFrame frame = new JFrame("ComboBox Test");
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.add(testApp.createControl());
                frame.setSize(600, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}