• 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

TableRowSorter issue

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a SimpleTableModel class which extends AbstractTableModel and EmployeeListView class in which i used SimpleTableModel object and added a TableRowSorter to it

im using hibernate to access database

In SimpleTableModel class im using 2 vectors to store and display results fetched from database
1st Vector to store all records in database
2nd vector to display a set of 25 or specified no of rows in Viewport


TableRowSorter sorts results on FirstName ie FirstName column
The problem is with TableRowSorter object sorter in EmployeeListView class it sorts n displays firstnames only out of displayed 25 rows (Maybe its bad coding logic of mine )
if i comment setDisplayData(fromIndex, toIndex); in SimpleTableModel and set 1st Vector as a datasource then all records are displayed in table n sorting occurs perfectly fine

My Requirement is :
1: to display 25 records is table allow pagination by 4 buttons next,previous,start,last [DONE]
2: to allow sorting by firstname on for all records in 1st Vector [with this im going ]

Any modifications or suggestions on implementation technique would be appreciated....

SimpleTableModel class



EmployeeListView class






 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Disclaimer: I've only scanned the code casually, so I may be wrong.

Your SimpleTableModel class uses dataVector in methods inherited defined by TableModel interface. Therefore the JTable can see - and sort - these records only. The JTable does not know anything about the allRows vector - it could get to know about it only if you passed information about allRows data to it using the TableModel's methods.

Don't sort the rows using TableRowSorter. Sort the rows directly in the allRows array. The pagination code can remain as it is. You may need to add some listeners to detect user clicking on the table header, sort the array accordingly and resend it to the table.

Another approach might be to use both table sorting and filtering capabilities and let the user filter the records by some criteria instead of pagination (see JTable tutorial for more info on filtering).
 
Shreyas Rane
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Martin , Thanks for your input..

When i use allRows Vector in methods inherited / defined by TableModel interface then my Table shows all the Rows (all Data present around 2000 records) and sorting takes place swiftly... [and hence paging is not required as all data is already displayed ]
my need is to display 25 records in table and when search happens on a particular employee firstname based on JCombobox result displayed in table should show all employees for that firstname

any suggestions are appreciated....
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't use bold / large / coloured fonts unless there is an actual need (e.g. to emphasize things).
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shreyas Rane wrote:hen i use allRows Vector in methods inherited / defined by TableModel interface then my Table shows all the Rows (all Data present around 2000 records) and sorting takes place swiftly... [and hence paging is not required as all data is already displayed ]
my need is to display 25 records in table and when search happens on a particular employee firstname based on JCombobox result displayed in table should show all employees for that firstname


Well, I'd say displaying fixed number of records at a time is exactly what "pagination" means. So you probably do need pagination after all.

You already know the JTable will display all the records you'll serve it through the table model. In this case your table obviously needs to operate in two distinct modes (pagination mode and search mode). Either build a single table model which will serve the data according to the current mode (and call fireTableDataChanged refresh functions on mode switch), or create two distinct models (different classes/instances) and use JTable.setModel() to switch between the modes.

These seem to be the most straightforward approaches to me, though there are certainly other possibilities.
 
Pay attention! Tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic