java - Merging two row filters for a JTable -
here code table
athleteid.setlocation(200,120); //sets location athleteid.setsize(150,26); //sets size athleteid.settext("athlete id search:"); firstpanel.add(athleteid); //adds panel forename.setlocation(731,120); //sets location forename.setsize(150,26); //sets size forename.settext("athlete name search:"); firstpanel.add(forename); //adds panel string[] aheaders = {"athlete id","forename","surname","on team"}; model = new defaulttablemodel(a.comptabledata,aheaders) { @override public boolean iscelleditable(int row, int column) { return false; } }; athtable = new jtable(model); athtable.setautocreaterowsorter(true); athtable.setrowheight(20); myscrolltable = new jscrollpane(athtable); myscrolltable.setsize(1082,600); myscrolltable.setlocation(200,145); system.out.println("creating compare table"); sorter = new tablerowsorter(athtable.getmodel()); list sortkeys = new arraylist(); sortkeys.add(new rowsorter.sortkey(3, sortorder.descending)); sorter.setsortkeys(sortkeys); //////// sorter.setrowfilter ( new rowfilter<tablemodel, integer>() { @override public boolean include(rowfilter.entry<? extends tablemodel, ? extends integer> entry) { boolean included = true; object cellvalue = entry.getmodel().getvalueat(entry.getidentifier(), 0); if (cellvalue == null || cellvalue.tostring().trim().isempty()) { included = false; } return included; } } ); ///////// sorter.setcomparator(0, new alphanumcomparator()); athtable.setrowsorter(sorter); filterathid = new jtextfield(10); filterathid.setsize(425,26); filterathid.setlocation(306,120); filterathid.settooltiptext("enter athlete id"); firstpanel.add(filterathid); filterforename = new jtextfield(10); filterforename.setsize(425,26); filterforename.setlocation(857,120); filterforename.settooltiptext("enter athlete name"); firstpanel.add(filterforename); btnback.setlocation(0,120); btnback.setsize(200,50); btnback.settext("back"); btnback.addactionlistener(this); firstpanel.add(btnback); document doc = filterathid.getdocument(); documentlistener listener = new documentlistener() { @override public void insertupdate(documentevent e) { newfilter(); } @override public void removeupdate(documentevent e) { newfilter(); } @override public void changedupdate(documentevent e) { newfilter(); } }; doc.adddocumentlistener(listener); document docb = filterforename.getdocument(); documentlistener listenerb = new documentlistener() { @override public void insertupdate(documentevent e) { newfilter(); } @override public void removeupdate(documentevent e) { newfilter(); } @override public void changedupdate(documentevent e) { newfilter(); } }; docb.adddocumentlistener(listenerb); for(int i=0;i<a.nextposition;i++) { system.out.println(athtable.getmodel().getvalueat(i,0)); } firstpanel.add(myscrolltable); } private void newfilter() { rowfilter rf = null; try { list<rowfilter<object,object>> filters = new arraylist<rowfilter<object,object>>(2); filters.add(rowfilter.regexfilter("(?i)"+filterathid.gettext(), 0)); filters.add(rowfilter.regexfilter("(?i)"+filterforename.gettext(), 1)); rf = rowfilter.andfilter(filters); } catch (java.util.regex.patternsyntaxexception e) { return; } sorter.setrowfilter(rf); }
the problem when search in filterathid
or filterforename
, remove contents searching blank rows appear in code.
sorter.setrowfilter ( new rowfilter<tablemodel, integer>() { @override public boolean include(rowfilter.entry<? extends tablemodel, ? extends integer> entry) { boolean included = true; object cellvalue = entry.getmodel().getvalueat(entry.getidentifier(), 0); if (cellvalue == null || cellvalue.tostring().trim().isempty()) { included = false; } return included; } } );
i had used code remove blank rows after empty search seems stop working. issue?
merging 2 row filters jtable
you can combine multiple filters one. example uses "and" fileter, can use "or" filter:
list<rowfilter<object,object>> filters = new arraylist<rowfilter<object,object>>(2); filters.add(rowfilter.regexfilter(filtertext.gettext(), 0)); filters.add(rowfilter.regexfilter(filtertext.gettext(), 1)); rf = rowfilter.andfilter(filters);
this example based on sorting , filtering demo swing tutorial.
Comments
Post a Comment