How can I update EventList
list to update JTable
? This is what I do:
String[] headers = new String[]{"MNO", "NAME", "ID/REG No", "PHONE"};
String[] properties = new String[]{"milkNo", "fullName", "nationalId", "phone1"};
TextFilterator<Member> personTextFilterator = new TextFilterator<Member>() {
@Override
public void getFilterStrings(List list, Member m) {
list.add(m.getFullName());
list.add(m.getMilkNo());
list.add(m.getNationalId());
list.add(m.getPhone1());
}
};
MatcherEditor<Member> textMatcherEditor = new TextComponentMatcherEditor<Member>(txtFilter, personTextFilterator);
FilterList<Member> filterList = new FilterList<Member>(eventList, textMatcherEditor);
TableFormat tf = GlazedLists.tableFormat(properties, headers);
model = new EventTableModel<Member>(filterList, tf);
selectionModel = new EventSelectionModel<Member>(filterList);
tblMembers.setSelectionModel(selectionModel);
tblMembers.setModel(model);
The probem is when i filter the records in table, and select a record and trying to update it, it create a new record in the table instead
int updatedRow = tblMembers.convertRowIndexToModel(tblMembers.getSelectedRow());
eventList.set(updatedRow, updatedMember);
Once you're using GlazedLists to power your
JTable
then you should only useEventTableModel
andEventSelectionModel
to interact with yourJTable
. The mistake you've made is to query the JTable directly for the selected row and get its index - but the JTable doesn't understand that it's backed by an EventList which maybe sorting/filtering items within. Therefore row i in the JTable will not necessarily correspond to element i in your EventList.In your code you have actually set up an EventSelectionModel, now it's just a matter of using it.
You have to remember that JTables support multiple row selections (although that can be configured allow only single row selection) and therefore the EventSelectionModel sensibly will return an EventList of all the selected rows, even if only one is selected. Therefore you need to iterate over the returned list. There is an additional convenience that the EventList containing the selected items is backed by the source EventList, so you can make changes directly to that sub-list.
Advanced
Of course EventLists are great for observing the fundamental changes to a list: deletion, insertion, updating (ie replacing an existing object at index i with another object). However, wouldn't it be great if instead of calling
selectedMembers.set()
, we could update an object within an EventList directly and then let GlazedLists detect that property change?Well that's possible too. There's a useful list type called
ObservableElementList
and here it will listen to property changes for each object it contains and refresh accordingly. This is much more convenient than having to locate the object within the list and calledeventList.set()
.You first need to make your class support property listeners, i.e., is a proper Java bean, and then use a
ObservableElementList.Connector
.I've created a small but complete example of this in action. In this example there is a text input at the top for filtering, a table containing a short list of authors, and a button at the bottom which will update the name of any selected row.