按字母顺序排列 JComboBox 元素
Order JComboBox Elements in Alphabetical Order
以下方法来自扩展 DefaultComboBoxModel
的 class:
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);
}
}
}
}
说明:我在这里所做的是分析我从包含多个元素的 xml 文件中获得的信息。这是一个例子:
<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>
关注第一行。看到参数group
了吗?这就是搜索的基础。如果为空,则表示该元素属于 default
组。如果它不为空,则意味着我应该将它作为一个新组添加到带有图标的组的 JComboBox 中(JComboBox 的每个元素都有一个图标和一个引用其名称的字符串)。
除一件小事外,一切正常。元素未按字母顺序排序,这不实用。
我考虑过使用 Collections.sort(),但这里的问题是我没有列表或数组。我有一个 DefaultComboBoxModel
没有方法可以按这种方式对其元素进行排序。
有什么建议吗?
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.
所以你:
- 将元素从 DefaultComboBoxModel 复制到数组
- 对数组进行排序
使用数组重新创建模型。
将模型添加到组合框
以下方法来自扩展 DefaultComboBoxModel
的 class:
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);
}
}
}
}
说明:我在这里所做的是分析我从包含多个元素的 xml 文件中获得的信息。这是一个例子:
<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>
关注第一行。看到参数group
了吗?这就是搜索的基础。如果为空,则表示该元素属于 default
组。如果它不为空,则意味着我应该将它作为一个新组添加到带有图标的组的 JComboBox 中(JComboBox 的每个元素都有一个图标和一个引用其名称的字符串)。
除一件小事外,一切正常。元素未按字母顺序排序,这不实用。
我考虑过使用 Collections.sort(),但这里的问题是我没有列表或数组。我有一个 DefaultComboBoxModel
没有方法可以按这种方式对其元素进行排序。
有什么建议吗?
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.
所以你:
- 将元素从 DefaultComboBoxModel 复制到数组
- 对数组进行排序
使用数组重新创建模型。
将模型添加到组合框