0

i'm trying to get the names and IDs from database and add them to combobox. i've added this checkcombobox to my project;

http://chianti.ucsd.edu/svn/csplugins/trunk/soc/jgao/IDMapping/src/csplugins/id/mapping/ui/CheckComboBox.java

by the help of this code; https://stackoverflow.com/a/17097767

            ResultSet resultSet= "here getting the result set with query having a ORDER BY clause   

            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                System.out.println(name);
                options.add(new Option<Integer>(name, id));
            }

system.out.println gives the output sorted by name but the items in checkcombobox are not sorted. how can i add the items sorted in combobox?

Community
  • 1
  • 1
echbel
  • 79
  • 1
  • 1
  • 10

2 Answers2

1

Here's a way to sort items in a combox box.

First Make your Option sortable by either using a Comparator or implementing the Comparable interface, then

  • First remove all the items.
  • Sort the list
  • Then add the items back from the sorted list.

    Collections.sort(options);
    comboBox.removeAllItems();
    for (Option s : options) {
        comboBox.addItem(s);
    }
    

Complete example

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SortCombo {

    JComboBox comboBox;
    JButton sortButton;
    List<Option> options;

    public SortCombo() {
        options = createOptions();

        comboBox = new JComboBox(options.toArray());
        sortButton = new JButton("Sort");

        sortButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Collections.sort(options);
                comboBox.removeAllItems();
                for (Option s : options) {
                    comboBox.addItem(s);
                }
            }
        });
        JFrame frame = new JFrame("Sort ComboBox");
        frame.add(comboBox, BorderLayout.CENTER);
        frame.add(sortButton, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public List<Option> createOptions() {
        List<Option> list = new ArrayList<>();
        list.add(new Option("John", 1));
        list.add(new Option("Sean", 2));
        list.add(new Option("Jake", 3));
        list.add(new Option("Mike", 4));
        list.add(new Option("Abby", 5));
        list.add(new Option("Paul", 6));
        list.add(new Option("Daniel", 7));
        list.add(new Option("Chris", 8));
        return list;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SortCombo();
            }
        });
    }
}

class Option implements Comparable<Option> {

    private String name;
    private int id;

    public Option(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public int compareTo(Option option) {
        return this.name.compareTo(option.name);
    }

    @Override
    public String toString() {
        return name + " {" + id + "}";
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0
  1. Don't use the Set constructor, use the CheckComboBox(Map<Object, Boolean> mapObjSelected) constructor.
  2. Use a TreeMap. The Set constructor creates a LinkedHashMap by default, which doesn't preserve order.
  3. Properly implement the compareTo method in the code for Option. You may need to add an extra field to tell the Options what order they're supposed to be in.
durron597
  • 31,968
  • 17
  • 99
  • 158