I'm working with JTables to display information that users can filter, and if the user saves after filtering I want to save the filtered table to a textfile for persistence (meaning anything that got filtered out will not be saved to the textfile).
For filtering I just followed the filtering part of this tutorial: http://download.oracle.com/javase/tutorial/uiswing/components/table.html#sorting and it works fine, but I'm not sure of any way that I can get a model of the current display as opposed to the underlying model that contains everything that hasn't been filtered out.
Is there any way to do this with this with the way that I'm filtering?
Thanks!
This code shows how to do this. Please note that B row is not printed to the input after the button is pressed.
Ask the table its number of rows (using
getRowCount()
), which will return the number of filtered (visible) rows. Iterate from 0 to the rowCount, convert each row index to the model index usingconvertRowIndexToModel()
, and ask your model the data at each model index to build the list of filtered (visible) data.