• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get specified no. of rows from jtable  RSS feed

 
Ashwin Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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 ?
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashwin Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

I am using java1.5, need to check if RowFilter works with 1.5
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't unfortunately.

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 ]
 
Ashwin Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Thanks
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!