troubleshooting Question

How can I sort a JList alphabetically?

Avatar of Joran Ottens
Joran OttensFlag for Netherlands asked on
JavaJavaScript
2 Comments1 Solution446 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
dpearson

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros