【问题标题】:How to have the ComboBox items sorted如何对 ComboBox 项目进行排序
【发布时间】:2013-06-08 03:40:29
【问题描述】:

我想在我的 vaadin 组合框中有一个以升序方式排序的项目列表。我正在添加如下项目。

     for (long i = 1; i < 11; i++) {
            Long item = new Long(i);
            comboBoxPriority.addItem(item);

        }

我也尝试过以下方式。我仍然得到一个按降序排列的项目列表。

for (long i = 10; i > 0; i--) {
                Long item = new Long(i);
                comboBoxPriority.addItem(item);

            }

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing sorting combobox vaadin


【解决方案1】:

您可以简单地将值添加到 List 并使用 Collections API 对其进行排序。

List<Long> values = new ArrayList<Long>(10);
for (long i = 10; i > 0; i--) {
    values.add(i);
}
Collections.sort(values);
DefaultComboBoxModel model = new DefaultComboBoxModel(values.toArray(new Long[values.size()]));
comboBoxPriority.setModel(model);

你可以使用数组和Arrays.sort 来实现同样的事情,如果这样更容易的话

【讨论】:

    【解决方案2】:

    一种方法是将数据放入 IndexedContainer,对数据进行排序,然后将数据添加到 ComboBox。请参阅 vaadin-forum 中 Charles Anthony 的示例。

    这是他的例子:

    /* Creating a container, with a property of "name". Item Id is a number, here. Can be anything (unique).
    * Alternatively, you could use the IndexedContainer to generate it's own ItemId :
    * cityContainer.getItem(cityContainer.addItem()).getItemProperty("name").setValue("New York");
    */
    IndexedContainer cityContainer = new IndexedContainer();
    cityContainer.addContainerProperty("name", String.class, null);
    cityContainer.addItem(1).getItemProperty("name").setValue("New York");
    cityContainer.addItem(2).getItemProperty("name").setValue("Turku");
    cityContainer.addItem(3).getItemProperty("name").setValue("Paris");
    cityContainer.addItem(4).getItemProperty("name").setValue("Zanzibar");
    cityContainer.addItem(5).getItemProperty("name").setValue("Turin");
    cityContainer.addItem(6).getItemProperty("name").setValue("London");
    cityContainer.getItem(cityContainer.addItem()).getItemProperty("name").setValue("New York");
    /* Lets sort the container on ascending name*/
    cityContainer.sort(new Object[]{"name"}, new boolean[]{true});
    
    /* Here's a comboBox that uses that container, where we are using the "name" property as the item caption */
    ComboBox comboBox = new ComboBox("City", cityContainer);
    comboBox.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
    comboBox.setItemCaptionPropertyId("name");
    

    【讨论】:

    【解决方案3】:

    似乎在这里工作得很好:

    import java.awt.*;
    import javax.swing.*;
    
    class ReversCombo {
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    JPanel gui = new JPanel(new GridLayout(1,0,5,5));
    
                    JComboBox comboBoxPriority = new JComboBox();
                    for (long i = 1; i < 11; i++) {
                        Long item = new Long(i);
                        comboBoxPriority.addItem(item);
                    }
    
                    JComboBox comboBoxPriority2 = new JComboBox();
                    for (long i = 10; i > 0; i--) {
                        Long item = new Long(i);
                        comboBoxPriority2.addItem(item);
                    }
    
                    gui.add(comboBoxPriority);
                    gui.add(comboBoxPriority2);
    
                    JOptionPane.showMessageDialog(null, gui);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }
    

    【讨论】:

    • 抱歉,我想在 Vaadin 中使用它,而不是在摇摆中。
    • “抱歉,我想在 Vaadin 中使用它” 抱歉,在您发布您自己的基于 Vaadin 的 SSCCE 之前(我要求您在 之前发布 制作我自己的 SSCCE),我无法提供进一步的帮助。
    • @Sanjaya Liyanage -1 表示忽略,in Vaadin are similair APIs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 2014-03-07
    相关资源
    最近更新 更多