1

I want to have a sorted item list in ascending manner in my vaadin combo box. I am adding items as below.

     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);

            }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50

3 Answers3

8

You could simply add the values to a List and uses the Collections API to sort it.

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
4

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.

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");
Andreas
  • 5,393
  • 9
  • 44
  • 53
2

Seems to work just fine here:

enter image description 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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433