0

I am having trouble implementing a JRadioButton to sort a JList of objects when a particular radio button is selected.

I have a custom Comparator:

public class IDComparator implements Comparator<Object> {
    public int compare(Object t, Object t1) {
        return t.getId().compareTo(t1.getId());
    }
}

Now I want to call that method onto the list of objects that I have in my frame to sort that list of objects.

I have tried something like...

   private void jRadioButton1ActionPerformed(ActionEvent evt) {                                              
        if (jRadioButton1.isSelected()) {
            Collections.sort(list, new IDComparator());
        }
    }     

While implementing action listeners etc. but I think this is just sorting the list before it goes into the list(?). So it isn't much help to me.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • [This example](https://stackoverflow.com/questions/35666648/sortedlistmodel-and-jlist-numerical-sort/35676131#35676131) and [this example](https://stackoverflow.com/questions/34735325/how-to-sort-a-string-array-within-a-gui/34735734#34735734) both demonstrate a concept of a `SortableListModel` – MadProgrammer Jun 02 '18 at 01:28
  • 1) This problem has as much to do with a `JRadioButton` as it does with the `JFrame` that is displaying the button and list. No need to mention it in the question title. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) Consider using a single column `JTable` instead, like in [this answer](https://stackoverflow.com/a/28621618/418556). The table API has inbuilt support for (filtering, in the linked example, &) sorting table data. – Andrew Thompson Jun 02 '18 at 05:07

0 Answers0