java - Unable to sort the JTable by Date -
i have jtable, first column of date. however, not date
object, string
display date in uk format. below code
private class displayallrevenue extends componentadapter { @override public void componentshown(componentevent e) { defaulttablemodel model = (defaulttablemodel) allrevenuetable.getmodel(); model.setrowcount(0); dbconnector = new dbhandler(); dbconnector.makeconnection(); java.sql.date dateoflastupdate=null; resultset portfoliors = dbconnector.selectalldetails(getportfoliodata); seek { if(portfoliors.isbeforefirst()==false) { joptionpane.showmessagedialog(null,"empty"); } else { while(portfoliors.next()) { string provider = portfoliors.getstring("provider name"); string client = portfoliors.getstring("client name"); int idportfolio = portfoliors.getint("idportfolio"); dateoflastupdate = portfoliors.getdate("update_date"); string dateoflastupdates = getdateinukformat(convertsqldatetojavadate(dateoflastupdate)); object[]row3 = {dateoflastupdates, provider, client, idportfolio}; model.addrow(row3); } } //sort table defaultrowsorter sorter = ((defaultrowsorter)allrevenuetable.getrowsorter()); arraylist list = new arraylist(); list.add( new rowsorter.sortkey(0, sortorder.descending) ); sorter.setsortkeys(list); sorter.sort(); } } catch(sqlexception sql) { joptionpane.showmessagedialog(null,"error displaying data"); sql.printstacktrace(); } { dbconnector.closeconnection(); } } }
as can see, trying sort table date
. unfortunately, didn't work! went out of order. wondering because date
string
.
so, how can sort table "correctly" according date ?
i wondering because date string.
yes, likely. note don't have mix info contained in table model representation. in case have date
object , show in uk format or whatever format like. better, internationalization trade allow table cell renderer/editor resolve current locale , apply date format accordingly.
the whole matter retrieving appropriate class in table model implementation overriding getcolumnclass(columnindex) method. if correctly, jtable
component able to:
for improve explanation see sorting , filtering section of how utilize tables tutorial. in nutshell:
to determine comparator
utilize column, tablerowsorter
attempts apply each of next rules in turn. rules followed in order listed below; first rule provides sorter comparator
used, , remainining rules ignored.
setcomparator
, utilize comparator. if table model reports column info consists of strings (tablemodel.getcolumnclass
returns string.class
column), utilize comparator sorts strings based on current locale. if column class returned tablemodel.getcolumnclass
implements comparable
, utilize comparator sorts strings based on values returned comparable.compareto
. if string convertor has been specified table invoking setstringconverter
, utilize comparator sorts resulting string representations based on current locale. if none of previous rules apply, utilize comparator invokes tostring
on column info , sorts resulting strings based on current locale. since date
class implements comparable
interface, it's case of point 3. so, 1 time again, overriding getcolumnclass()
correctly lead solve problem.
please note database calls time consuming tasks , may block event dispatch thread (edt) causing gui become unresponsive. edt single , special thread swing components creation , update must performed , event handling take place.
having said that, take part in code:
private class displayallrevenue extends componentadapter { @override public void componentshown(componentevent e) { // event handling code: performed in edt } }
if create database calls every time component shown you'll have severe performance issues. might consider add together button allow users refresh table's info instead of trying automatically when component shown.
additionally, in order avoid blocking edt might consider utilize swingworker perform database calls in background thread , update swing components in edt. see more in concurrency in swing trail.
java swing sorting user-interface jtable
No comments:
Post a Comment