0

好吧。,我已经尝试了我的袖子里的每一个技巧。。但无法弄清楚如何更新组合框 w/glazedList ..如果输入来自其他类..我尝试将值传递给方法,首先声明它一个字符串..等等..但是没有一个有用..如果新项目将来自同一个类..通过单击一个按钮..到目前为止我得到了这个代码..

 values = GlazedLists.eventListOf(auto);//auto is an array..
    AutoCompleteSupport.install(comboSearch,values);//comboSearch is the comboBox

//"x" is the value coming from another class.

public void updateCombo(String x){
        List<String> item = new ArrayList<>();
        item.add(x)
        value.addAll(item);
 }

我希望这些代码足以解释我想问的问题..

4

1 回答 1

3

无法查看您是如何创建组合框和事件列表的。因此,我将从头开始创建一个简单的示例应用程序,向您展示基本要素。

万一您不熟悉一般概念,主要的要点是:

  • 尽量避免使用标准的 Java 集合(例如 ArrayList、Vector)并EventList尽快使用该类。排序/过滤/自动完成带来的所有好处都依赖于 EventList 基础,因此请尽快设置一个,然后简单地操作(添加/删除/等),然后 GlazedLists 管道将负责其余的工作。
  • 一旦你将你的对象集合放在一个中EventList并且你想利用一个摆动组件,然后查看ca.odell.glazedlists.swing包含你需要的一切的模块。在这种情况下,您可以在您的事件列表中使用EventListComboBoxModel- 传递,然后将您的 JComboBox 模型设置为使用新创建的模型,然后EventListComboBoxModelGlazedLists 将负责确保您的列表数据结构和组合框保持同步。

所以在我的例子中,我创建了一个空的组合框和一个按钮。单击该按钮将在每次单击时将一个项目添加到组合框中。神奇之处在于创建 EventList 并使用EventListComboBoxModel将列表链接到组合框。

请注意,下面的代码仅针对 GlazedLists 1.8 进行了测试。但我很确定它也适用于 1.9 或 1.7。

public class UpdateComboBox {

    private JFrame mainFrame;
    private JComboBox cboItems;
    private EventList<String> itemsList = new BasicEventList<String>();

    public UpdateComboBox() {
        createGUI();
    }

    private void createGUI() {
        mainFrame = new JFrame("GlazedLists Update Combobox Example");
        mainFrame.setSize(600, 400);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton addButton = new JButton("Add Item");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                itemsList.add("Item " + (itemsList.size()+1));
            }
        });

        // Use a GlazedLists EventComboBoxModel to connect the JComboBox with an EventList.
        EventComboBoxModel<String> model = new EventComboBoxModel<String>(itemsList);
        cboItems = new JComboBox(model);

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(cboItems, BorderLayout.NORTH);
        panel.add(addButton, BorderLayout.SOUTH);

        mainFrame.getContentPane().add(panel);
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
          new UpdateComboBox();
        }
    });
    }
}
于 2014-03-18T16:26:18.547 回答