java 如何对 ComboBox 项目进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17058514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have the ComboBox items sorted
提问by Sanjaya Liyanage
I want to have a sorted item list in ascending manner in my vaadin combo box. I am adding items as below.
我想在我的 vaadin 组合框中以升序方式排序项目列表。我正在添加如下项目。
for (long i = 1; i < 11; i++) {
Long item = new Long(i);
comboBoxPriority.addItem(item);
}
I also tried it below way. Still I am getting a item list in descending order.
我也试过下面的方式。我仍然按降序获取项目列表。
for (long i = 10; i > 0; i--) {
Long item = new Long(i);
comboBoxPriority.addItem(item);
}
回答by MadProgrammer
You could simply add the values to a List
and uses the Collections
API to sort it.
您可以简单地将值添加到 aList
并使用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);
You could achieve the same thing using an array and Arrays.sort
if that was eaiser
你可以使用数组来实现同样的事情,Arrays.sort
如果那更容易的话
回答by Andreas
One way would be to put the data into an IndexedContainer, sort the data, then add the data to the ComboBox. See Charles Anthony's example in the vaadin-forum.
一种方法是将数据放入 IndexedContainer,对数据进行排序,然后将数据添加到 ComboBox。请参阅vaadin-forum中的 Charles Anthony 示例。
Here is his example:
这是他的例子:
/* 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");
回答by Andrew Thompson
Seems to work just fine here:
似乎在这里工作得很好:
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);
}
}