• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get specified no. of rows from jtable

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic