Avatar of Joran Ottens
Joran Ottens
Flag for Netherlands asked on

How can I sort a JList alphabetically?

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
JavaJavaScript

Avatar of undefined
Last Comment
dpearson

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
dpearson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dpearson

Useful reference guide me thinks.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes