Java 在组合框中选择ascendingorder时如何对元素列表进行排序
我创建了一个组合,其中包含包含字符串的列表。我有一个组合框,其中有升序和降序选项。现在,如果我在组合框中选择升序,列表应该按字母顺序排序。那么如何做到这一点呢 例如,如果我的合成包含一个列表“some”、“zero”、“one” 然后在我的组合框中选择升序,它应该被排序并显示为一,一些,零 创建combobox的代码如下Java 在组合框中选择ascendingorder时如何对元素列表进行排序,java,combobox,swt,listener,e4,Java,Combobox,Swt,Listener,E4,我创建了一个组合,其中包含包含字符串的列表。我有一个组合框,其中有升序和降序选项。现在,如果我在组合框中选择升序,列表应该按字母顺序排序。那么如何做到这一点呢 例如,如果我的合成包含一个列表“some”、“zero”、“one” 然后在我的组合框中选择升序,它应该被排序并显示为一,一些,零 创建combobox的代码如下 String[] ITEMS1 = {"A-Z", "Z-A" }; comboSort = new Combo(comboComposite, SWT.NONE);
String[] ITEMS1 = {"A-Z", "Z-A" };
comboSort = new Combo(comboComposite, SWT.NONE);
comboSort.setBounds(84, 2, 91, 23);
comboSort.setItems(ITEMS1);
tabFolder = new TabFolder(topComposite, SWT.NONE);
GridData tabFolderGD = new GridData(SWT.FILL, GridData.FILL, true, true);
tabFolderGD.verticalIndent = 4;
tabFolderGD.horizontalSpan = 2;
tabFolder.setLayoutData(tabFolderGD);
TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
tabItem.setText("My created");
createListViewMycreated(tabFolder,tabItem);
private void createListViewMycreated(Composite composite, TabItem ItemMycreated){
List myCreatedList = new List(composite,SWT.BORDER);
myCreatedList.setItems(new String[]{"CompSetup_SEMCW8459_TiffanyA005 ","Product_Sirius_Perch_OEM0_AID1_00440245156645 ","SW_SEC Test_DEMO_Sirius "});
ItemMycreated.setControl(myCreatedList);
setDragSource(myCreatedList);
comboSort.addSelectionListener(new SelectionListener() {
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
System.out.println("hi you selected me in combo box"+comboSort.getText());
}
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
System.out.println("hi you selected me in combo box"+comboSort.getText());
String ascending =comboSort.getText();
if (ascending== "A-Z")
{
}
}
});
}
tabitem和监听器的代码如下
String[] ITEMS1 = {"A-Z", "Z-A" };
comboSort = new Combo(comboComposite, SWT.NONE);
comboSort.setBounds(84, 2, 91, 23);
comboSort.setItems(ITEMS1);
tabFolder = new TabFolder(topComposite, SWT.NONE);
GridData tabFolderGD = new GridData(SWT.FILL, GridData.FILL, true, true);
tabFolderGD.verticalIndent = 4;
tabFolderGD.horizontalSpan = 2;
tabFolder.setLayoutData(tabFolderGD);
TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
tabItem.setText("My created");
createListViewMycreated(tabFolder,tabItem);
private void createListViewMycreated(Composite composite, TabItem ItemMycreated){
List myCreatedList = new List(composite,SWT.BORDER);
myCreatedList.setItems(new String[]{"CompSetup_SEMCW8459_TiffanyA005 ","Product_Sirius_Perch_OEM0_AID1_00440245156645 ","SW_SEC Test_DEMO_Sirius "});
ItemMycreated.setControl(myCreatedList);
setDragSource(myCreatedList);
comboSort.addSelectionListener(new SelectionListener() {
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
System.out.println("hi you selected me in combo box"+comboSort.getText());
}
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
System.out.println("hi you selected me in combo box"+comboSort.getText());
String ascending =comboSort.getText();
if (ascending== "A-Z")
{
}
}
});
}
那么现在我们如何根据组合框选择对列表进行排序呢?我认为myCreatedList有一个字符串列表
Collections.sort(myCreatedList);
如果它在下降,那么
Collections.reverse(myCreatedList);
有关收藏的更多信息
这可能会起到以下作用:
// Re-sort
String[] items = combo.getItems();
Arrays.sort(items);
combo.setItems(items);
@SrikanthBadida哦,好吧,我以为你只需要分类的东西。请勾选这个,这可能有助于你编写代码