Is there any way to do it?
+2
A:
The best way would be to remove the corresponding element from the model.
Michael Borgwardt
2009-06-03 10:21:02
But if I need to show him again later?
joycollector
2009-06-03 10:25:06
Then you add it again :)
willcodejavaforfood
2009-06-03 10:27:50
Finally I did this way... Two dataSets... One is full, the other one populates when fireDataTableChanged called... But I'm afraid it's slow...
joycollector
2009-06-04 05:02:23
+2
A:
Check out Sun's Tutorial for JTables and look at the Sorting and Filtering section.
willcodejavaforfood
2009-06-03 10:28:41
JXTable from swinglabs if you want to try filtering in <1.6. You can also make your own TableModel that decorates another TableModel with sorting functions.
KitsuneYMG
2009-06-03 12:56:42
A:
You could create a wrapper around TableModel with methods like setVisible(int tableRow);
when asked for rows, it would return only those rows that were visible.
Alex Feinman
2009-06-03 19:59:03
Hmm... What method in TableModel asks for row? In table model we can only override getValueAt and getRowCount (and of course setValueAt)... I thought about it but found it impossible...
joycollector
2009-06-04 04:58:52
Yes of course, but even if you set all cells in this row to null it will be diaplayed as row with empty cells...
joycollector
2009-06-05 04:43:17
Ah, but you don't return null for hidden rows; you skip over them when you count rows.E.g., if you hid row 2, and someone called getValueAt(2, 1), you would return row 3 data instead.
Alex Feinman
2009-06-05 12:48:08
+1
A:
There is the RowFilter<DefaultTableModel, Object>
class you can use to filter out rows. The DefaultTableModel can be replaced by your own model. To filter, implement the method
@Override
public boolean include(Entry entry) {
// All rows are included if no filter is set
if (filterText.isEmpty())
return true;
// If any of the column values contains the filter text,
// the row can be shown
for (int i = 0; i < entry.getValueCount(); i++)
{
String value = entry.getStringValue(i);
if (value.toLowerCase().indexOf(filterText) != -1)
return true;
}
return false;
}
When accessing rows, for instance listening to ListSelectionEvents, do not forget to translate the visible row index to the complete row index in your model. Java provides a function for this as well:
public void valueChanged(ListSelectionEvent e)
{
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
int visibleRowIndex = ... // Get the index of the selected row
// Convert from the selection index based on the visible items to the
// internal index for all elements.
int internalRowIndex = tableTexts.convertRowIndexToModel(visibleRowIndex);
...
}
Thomas Feilkas
2009-10-08 06:04:23