【问题标题】:how to sort the list of elements when ascendingorder is selected in the combobox在组合框中选择升序时如何对元素列表进行排序
【发布时间】:2015-02-21 07:29:03
【问题描述】:

我创建了一个组合,其中包含包含字符串的列表。我有一个组合框,其中包含选项升序和降序。所以现在如果我在组合框中选择升序,则列表应按字母顺序排序。那么如何做到这一点。

例如,如果我的合成包含列表“一些”、“零”、“一个” 并在我的组合框中选择升序,它应该被排序并显示为一,一些,零。

创建组合框的代码如下

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

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")
                {

                }
            }


            });

    }

那么现在我们如何根据组合框选择对列表进行排序

【问题讨论】:

    标签: java combobox swt listener e4


    【解决方案1】:

    我认为你的 myCreatedList 有字符串列表。如果你想按升序排序

    Collections.sort(myCreatedList);
    

    如果它是下降的那么

    Collections.reverse(myCreatedList);
    

    有关收藏的更多信息

    check this

    【讨论】:

    • @SrikanthBadida 哦,好吧,我以为你只需要排序的东西。请检查这个,这可能会帮助你编写代码Check this
    【解决方案2】:

    这可能会奏效:

          // Re-sort
          String[] items = combo.getItems();
          Arrays.sort(items);
          combo.setItems(items);
    

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多