With your columns prepared, replace the
JList
with a JTable
. This
means exchanging the EventListModel
with an
EventTableModel
, which requires your
IssueTableFormat
for its constructor. The
SortedList
is the data source for the
EventTableModel
.
Although it's initially sorted by priority, your users will want
to reorder the table by clicking on the column headers. For example,
clicking on the "Type" header shall sort the issues by type. For this,
Glazed Lists provides TableComparatorChooser
,
which adds sorting to a JTable
using your
SortedList
.
/** * Display a frame for browsing issues. */ public void display() { SortedList sortedIssues = new SortedList(issuesEventList, new IssueComparator()); // create a panel with a table JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); EventTableModel issuesTableModel = new EventTableModel(sortedIssues, new IssueTableFormat()); JTable issuesJTable = new JTable(issuesTableModel); TableComparatorChooser tableSorter = new TableComparatorChooser(issuesJTable, sortedIssues, true); JScrollPane issuesTableScrollPane = new JScrollPane(issuesJTable); 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(); }
TableComparatorChooser
supports both
single column sorting (simpler) and multiple column sorting (more
powerful). This is configured by the third argument in the
constructor.
By default, TableComparatorChooser
sorts
by casting column values to Comparable
. If your
column's values are not Comparable
, you'll have
to manually remove the default Comparator
using
TableComparatorChooser.getComparatorsForColumn(column).clear().