wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes TableRowSorter issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TableRowSorter issue" Watch "TableRowSorter issue" New topic
Author

TableRowSorter issue

Shreyas Rane
Greenhorn

Joined: Apr 10, 2009
Posts: 16
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






Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Apr 10, 2009
Posts: 16
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....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Please don't use bold / large / coloured fonts unless there is an actual need (e.g. to emphasize things).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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.
 
wood burning stoves
 
subject: TableRowSorter issue
 
Similar Threads
Change Text for JButton rendered in a JTable
Problem with JTable
Problem with Thread..
Printing invicible JTable with Column removed - JTable.PrintMode.FIT_WIDTH problem
JTable repaint issue