I have created a table with GlazedLists to be able to filter the table for searching purposes. Now the question is how to delete, add, or update rows in this table. I did search the API but couldn't figure it out.
Can anyone demonstrate how to achieve for deleting, adding, updating with a simple example?
public static JTable retrieveArtikelTable(String filePath, String[] header, JTextField filterField) {
EventList eventList = new BasicEventList();
String[] headers = new String[0];
CsvReader reader = null;
JTable t = new JTable();
if (filePath != null) {
if (new File(filePath).exists()) {
try {
reader = new CsvReader(filePath, ';');
if (header != null) {
headers = header;
} else {
headers = reader.getHeaders();
}
while (reader.readRecord()) {
String[] values = reader.getValues();
eventList.add(new Artikel(values[0], values[1], values[2]));
}
reader.close();
TextFilterator artikelFilterator = new TextFilterator() {
public void getFilterStrings(List baseList, Object element) {
Artikel artikel = (Artikel) element;
baseList.add(artikel.getCode());
baseList.add(artikel.getName());
baseList.add(artikel.getNumber());
}
};
TextComponentMatcherEditor matcherEditor = new TextComponentMatcherEditor(filterField, artikelFilterator);
FilterList filteredArtikels = new FilterList(eventList, new ThreadedMatcherEditor(matcherEditor));
// build a JTable
TableFormat tf = GlazedLists.tableFormat(Artikel.class,headers, headers);
t = new JTable(new EventTableModel(filteredArtikels, tf));
} catch (Exception ex) {
OeExceptionDialog.show(ex);
} finally {
assert reader != null;
reader.close();
}
}
}else{
t=new JTable();
JOptionPane.showMessageDialog(filterField.getParent(),"Attention: " +filePath+
"There is no such a file to be able to create a table!");
}
return t;
}
header = new String[]{"Code","Name","Number"};
JTextField filterField = new JTextField();
JTable table = retrieveArtikelTable("c:\articl.csv", header, filterField)
int selectedRow = table.getSelectedRow();
EventTableModel tableModel = (EventTableModel) table.getModel();
// code for deleting a row from this table
// code for add row to this table
// code for updating row from this table
Edited:
EventList eventList = new BasicEventList();
JTable table = retrieveArtikelTable("c:\articl.csv", header, filterField, eventList)
int selectedRow = table.getSelectedRow();
// code for deleting a row from this table
eventList.remove(selectedRow);
// code for add row to this table
// code for updating row from this table
If you make changes to the EventList then those changes will be propagated to the JTable via the table model. (That is, if you add a new object to that list, remove one, or update one, then the changes take place automatically. Hence the "Event" prefix - events to the list are communicated to the model.)
So chances are you'll need to keep a reference to your EventList to ensure its accessible to parts of your code outside of
retrieveArtikelTable()
in your case.I feel the screencasts at GlazedLists Developer are excellent at covering all the essential topics.
Edit: just a reminder how you can make the list an instance variable so that it can be accessed in any method, not just the one which constructs the table.
Edit #2: I've included a more complete example in order to give an illustration of how you should properly handle selection using the EventSelectionModel, which allows you to know exactly which rows are selected at any given time, even when a filter has been applied.
I've copied a file which was partially generated using the Netbeans GUI builder. But the key things to note are:
Declaration of the EventSelectionModel as an instance variable so that it can be accessed elsewhere in the class.
btnDeleteActionPerformed()
method. This is what happens when the Delete button is pressed. First I check whether any row is selected. If so, get the selected items (returned as an Eventlist) and simply remove them from the master list.Here's the sample code for MyFrame.java
I'm using a Person class in my example, and it's a very simple POJO:
Personally I'd recommend keeping your data loading separate from the method which creates your table. I've used the
loadData()
method which populates the list. If there's a no data to be loaded, or a problem with the file you're loading from then the table is still created but there's nothing in it because the list remains empty.