I want to retrieve the specified no. of rows from a given dummy jtable. This should be done according to the input we give in the textbox, For example if there are 100 rows in dummy table and if give 10 as the input, the table should display only 10rows. I have created a dummy jtable of 100 rows, and i can get the no. of rows required by hardcoding. But how to get by giving some input in gui with the dummy table ?
If you are using Java 6, you could use a RowFilter:
You will have to use a TableRowSorter in between but that shouldn't be a problem, since if you don't want that you can disable sorting using setSortable, or perhaps by overriding TableRowSorter and have isSortable return false regardless of the column.
But perhaps you can cheat a bit with your TableModel. Assuming you are using DefaultTableModel now*, subclass it:
You will probably have to tweak it a little bit, especially disallowing negative indexes.
This approach does not really delete or insert anything, but it makes the listeners (including the table) think it has.
* Actually any subclass of AbstractTableModel will work; all it needs is a non-final getRowCount method and the two fire methods. [ November 09, 2008: Message edited by: Rob Prime ]
Sorry for the delayed response...Somehow...the fireTableRowsDeleted() method is not working for me...Do i need to use the table.repaint() method after this so that the JTable display gets reflected...
Also please let me know if there is any method which can be used for displaying a jtable with user input like say, startRow=3 and EndRow=15 from a table of 100 rows.
It's strange that fireTableRowsDeleted doesn't work. What do you get when you print the result of getRowCount just before that call? If that still won't work you can always call fireTableDataChanged() or even fireTableStructureChanged(), but I prefer to keep the events as specific as possible.
About the start and end, it would work the same but you'll have to override a lot more methods.
I've done just one to show how it could be done. Also, the count will be end - start (+1 if the end is inclusive).