0

The following method is from a class that extends DefaultComboBoxModel:

public void addGroups() {

    removeAllElements();
    ListModel listModel = ((AutoDataProvider) dataProvider).getXmlJobCustom().getJobList();
    ImageIcon imageIcon;
    GroupModel groupModel;
    for (int i = 0;i < listModel.getSize();i++) {
        XmlJobCustomElem elementAt = (XmlJobCustomElem) listModel.getElementAt(i);
        String group = elementAt.getGroup();

        if(StringUtils.isNotEmpty(group)) {
            if (group.equalsIgnoreCase("WebServices")){
                imageIcon = Res.getIcon(Constants.ICO + "$WS");
            }
            else if (group.equalsIgnoreCase("file transfer") || group.equalsIgnoreCase("transfert de fichiers")){
                imageIcon = Res.getIcon(Constants.ICO + "$FT");
            }
            else if (group.equalsIgnoreCase("json")){
                imageIcon =  Res.getIcon(Constants.ICO + "$JSON");
            }
            else if (group.toLowerCase().contains("aws")){
                imageIcon = Res.getIcon(Constants.ICO + "$AWS");
            } else {
                imageIcon = Res.getDefIcon(Res.getUserLocalPath() + "/" + XmlJobCustom.ICONS_DIR + "/" + XmlJobCustom.ICONS + elementAt.getId(), Constants.ICO + "$Job.Type." + Job.JOB_CUSTOM);
            }
            groupModel = new GroupModel(imageIcon, group);

            if (getIndexOf(groupModel) ==-1 ){
                addElement(groupModel);
            }
        } else if (StringUtils.isEmpty(group)){
            imageIcon = Res.getIcon(Constants.ICO + "$Job.Type" +"."+ 0);
            groupModel = new GroupModel(imageIcon, "Default");
            if (getIndexOf(groupModel) == -1 ){
                addElement(groupModel);
            }
        }
    }

}

Explanation: What I'm doing here is analyse the information that I'm getting from an xml file that contains several elements. Here's an example:

<Job id="job password" name="job password" group="AWS S3">
    <params>
        <param id="env" name="environment" desc="Python environment" default="default" mandatory="yes"/>
        <param id="passwordid" name="passwd" type="password" desc="passwd" mandatory="yes"/>
    </params>
    <actions>
        <action id="start">
            <params>
                <param prefix="env">env</param>
                <param prefix="--connector">Connector</param>
            </params>
        </action>
    </actions>
</Job>

Focus on the first line. Do you see the parameter group? That's what the search is based on. If it's empty, it means the element belongs to the default group. If it's not empty, it means I should add it as a new group to the JComboBox of groups with its icon (each element of the JComboBox has an Icon and a String that refers to its name).

Everything is working fine except for one little thing. The elements aren't sorted in alphabetical order which is not practical.

I thought about using Collections.sort(), but the problem here is that I don't have a list or an Array. I have a DefaultComboBoxModel that has no methods to sort its elements that way.

Any suggestions?

camickr
  • 321,443
  • 19
  • 166
  • 288
Jesse James
  • 1,203
  • 5
  • 22
  • 39

1 Answers1

1

but the problem here is that I don't have a list or an Array. I have a DefaultComboBoxModel that has no methods to sort its elements that way.

So you:

  1. copy the elements from the DefaultComboBoxModel to an Array
  2. sort the Array
  3. recreate the model using the Array.

  4. add the model to the combo box

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you. One tiny problem though, the elements of the DefaultComboBoxModel are instances of `GroupModel` ( a class that I created and which has an Icon and a String as its variables). I'm definitely missing something here, but I thought you can only use the `sort()` on Strings, not Objects? – Jesse James Jun 12 '20 at 16:38
  • You can sort any object if the object implements `Comparable` or if you create a `Comparator`. The choice is yours. See: https://stackoverflow.com/questions/1946668/sorting-using-comparator-descending-order-user-defined-classes/1947527#1947527 for a basic example of both approaches. – camickr Jun 12 '20 at 16:47
  • Thank you for your time. – Jesse James Jun 12 '20 at 16:48