Eliminating duplicates with UniqueList

Although our issues list contains over 100 issues, there are only a handful of users and each user is responsible for multiple issues. This means that our users list will contain duplicates. This problem is solved quickly and easily by UniqueList. Just like SortedList, the UniqueList uses Comparable elements or a Comparator to distinguish between elements.

    // derive the users list from the issues list
    EventList usersNonUnique = new IssueToUserList(issuesEventList);
    UniqueList usersEventList = new UniqueList(usersNonUnique);
      
    

Finally we add a JList to our user interface to display the list of users. In the next part of the tutorial, we will get this widget to act as a custom filter for the issues table.

  /**
   * Display a frame for browsing issues.
   */
  public void display() {
    SortedList sortedIssues = new SortedList(issuesEventList, new IssueComparator());
    TextFilterList textFilteredIssues = new TextFilterList(sortedIssues, new IssueTextFilterator());
    
    // derive the users list from the issues list
    EventList usersNonUnique = new IssueToUserList(issuesEventList);
    UniqueList usersEventList = new UniqueList(usersNonUnique);
    
    // create the issues table
    EventTableModel issuesTableModel = new EventTableModel(textFilteredIssues, new IssueTableFormat());
    JTable issuesJTable = new JTable(issuesTableModel);
    TableComparatorChooser tableSorter = new TableComparatorChooser(issuesJTable, sortedIssues, true);
    JScrollPane issuesTableScrollPane = new JScrollPane(issuesJTable);

    // create the users list
    EventListModel usersListModel = new EventListModel(usersEventList);
    JList usersJList = new JList(usersListModel);
    JScrollPane usersListScrollPane = new JScrollPane(usersJList);
    
    // create the panel
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    panel.add(new JLabel("Filter: "), new GridBagConstraints(...));
    panel.add(textFilteredIssues.getFilterEdit(), new GridBagConstraints(...));
    panel.add(new JLabel("Reported By: "), new GridBagConstraints(...));
    panel.add(usersListScrollPane, new GridBagConstraints(...));
    panel.add(issuesTableScrollPane, new GridBagConstraints(...));
    
    // create a frame with that panel
    JFrame frame = new JFrame("Issues");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setSize(540, 380);
    frame.getContentPane().add(panel);
    frame.show();
  }