【问题标题】:how to update comboBox while using glazedList..evenlist如何在使用 glazedList..evenlist 时更新组合框
【发布时间】:2014-02-05 08:28:51
【问题描述】:

好吧。,我已经尝试了所有技巧。但无法弄清楚如何更新带有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);
 }

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

【问题讨论】:

    标签: java combobox jcombobox auto-update glazedlists


    【解决方案1】:

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

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

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

    所以在我的示例中,我创建了一个空的组合框和一个按钮。单击该按钮将在每次单击时将一个项目添加到组合框中。神奇之处在于创建 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();
            }
        });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      相关资源
      最近更新 更多