We help IT Professionals succeed at work.
Get Started

How can I sort a JList alphabetically?

Joran Ottens
Joran Ottens asked
on
445 Views
Last Modified: 2017-05-03
public void OmhoogB_ActionPerformed(ActionEvent evt) {
        replace = Lijst.getSelectedIndex();
        if (Lijst.isSelectionEmpty()){
            WelkomL.setText("Niets geselecteerd");
            WelkomL.setForeground(Color.red);
        }
        if (replace != 0) {
            swap(replace, replace + 1);
            WelkomL.setText("Gelukt");
            WelkomL.setForeground(Color.green);
        }
        else{
            WelkomL.setText("Kan niet verder omhoog");
            WelkomL.setForeground(Color.RED);
        }
    } // end of OmhoogB_ActionPerformed
    public void BenedenB_ActionPerformed(ActionEvent evt) {
        replace = Lijst.getSelectedIndex();
        if (Lijst.isSelectionEmpty()){
            WelkomL.setText("Niets geselecteerd");
            WelkomL.setForeground(Color.red);
        }
        if (replace != (Count-1)) {
            swap(replace, replace + 1);
            WelkomL.setText("Gelukt");
            WelkomL.setForeground(Color.green);
        }
        else{
            WelkomL.setText("Kan niet verder omlaag");
            WelkomL.setForeground(Color.RED);
        }
    } // end of BenedenB_ActionPerformed
    public void SorteerMerkB_ActionPerformed(ActionEvent evt) {
        MyComparator cmp = new MyComparator();
// sort my arraylist named aList
        Collections.sort(aList, cmp);

        DefaultComboBoxModel model = new DefaultComboBoxModel(aList);
        JList list = new JList();
        list.setModel(model);

        private class MyComparator implements Comparator {
            // compares strings
            public int compare (Object a, Object b) {
                String x = (String) a;
                String y = (String) b;

                return x.compareTo(y);
            }
        }



the last button has to sort the JList alphabetically
Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE