java - This code to filter data for javax swing table.After run it when I press OK, it show 0 data.but it starts work from 2nd press on OK button -
import java.awt.eventqueue; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import javax.swing.joptionpane; import javax.swing.jscrollpane; import javax.swing.rowfilter; import javax.swing.table.defaulttablemodel; import javax.swing.table.tablerowsorter; import java.awt.event.actionlistener; import java.awt.event.actionevent; public class extends javax.swing.jframe { defaulttablemodel model; connection connect; statement s; resultset rst; string expr; final tablerowsorter<defaulttablemodel> sorter; public my() { initcomponents(); model = (defaulttablemodel)jtable1.getmodel(); sorter = new tablerowsorter<defaulttablemodel> (model); seek { class.forname("com.mysql.jdbc.driver"); connect = drivermanager.getconnection("jdbc:mysql://localhost/sorts","root",null); s = connect.createstatement(); string sql = "select * member"; resultset rec = s.executequery(sql); int row = 0; while((rec!=null) && (rec.next())) { model.addrow(new object[0]); model.setvalueat(rec.getstring("membershipno"), row, 0); model.setvalueat(rec.getstring("name"), row, 1); model.setvalueat(rec.getstring("workingstation"), row, 2); model.setvalueat(rec.getstring("address"), row, 3); model.setvalueat(rec.getstring("teleno"), row, 4); model.setvalueat(rec.getstring("email"), row, 5); row++; } rec.close(); } grab (exception e) { joptionpane.showmessagedialog(null, e.getmessage()); e.printstacktrace(); } seek { if(s != null) { s.close(); connect.close(); } } grab (sqlexception e) { e.printstacktrace(); } jtable1.setrowsorter(sorter); } @suppresswarnings("unchecked") private void initcomponents() { jscrollpane1 = new javax.swing.jscrollpane(); system.out.println("3"); jtable1 = new javax.swing.jtable(); jbutton1 = new javax.swing.jbutton(); jtextfield1 = new javax.swing.jtextfield(); jpanel1 = new javax.swing.jpanel(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); system.out.println("4"); jtable1.setmodel(new javax.swing.table.defaulttablemodel( new object [][] { }, new string [] { "title 1", "title 2", "title 3", "title 4", "title 5", "title 6" } ) { class[] types = new class [] { java.lang.string.class, java.lang.string.class, java.lang.string.class, java.lang.string.class, java.lang.object.class, java.lang.object.class }; public class getcolumnclass(int columnindex) { homecoming types [columnindex]; } }); system.out.println("5"); system.out.println("2"); jscrollpane1.setviewportview(jtable1); system.out.println("7"); jbutton1.settext("jbutton1"); jbutton1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbutton1actionperformed(evt); } }); jtextfield1.settext(" "); jtextfield1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jtextfield1actionperformed(evt); } }); javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout(jpanel1); jpanel1.setlayout(jpanel1layout); jpanel1layout.sethorizontalgroup( jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 100, short.max_value) ); jpanel1layout.setverticalgroup( jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 100, short.max_value) ); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 375, javax.swing.grouplayout.preferred_size) .addgap(0, 25, short.max_value)) .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(47, 47, 47) .addcomponent(jtextfield1, javax.swing.grouplayout.preferred_size, 125, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value)) .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() .addcontainergap(javax.swing.grouplayout.default_size, short.max_value) .addcomponent(jpanel1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addgap(18, 18, 18))) .addcomponent(jbutton1) .addgap(111, 111, 111)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 143, javax.swing.grouplayout.preferred_size) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(60, 60, 60) .addcomponent(jbutton1)) .addgroup(layout.createsequentialgroup() .addgap(51, 51, 51) .addcomponent(jtextfield1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jpanel1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size))) .addgap(0, 11, short.max_value)) ); pack(); } private void jbutton1actionperformed(java.awt.event.actionevent evt) { if(evt.getsource()==jbutton1){ expr = jtextfield1.gettext(); filter(); sorter.setsortkeys(null); jtextfield1.settext(""); } } private void jtextfield1actionperformed(java.awt.event.actionevent evt) { } protected void filter() { sorter.setrowfilter(rowfilter.regexfilter("^(?i)"+expr)); } seek { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } grab (classnotfoundexception ex) { java.util.logging.logger.getlogger(my.class.getname()).log(java.util.logging.level.severe, null, ex); } grab (instantiationexception ex) { java.util.logging.logger.getlogger(my.class.getname()).log(java.util.logging.level.severe, null, ex); } grab (illegalaccessexception ex) { java.util.logging.logger.getlogger(my.class.getname()).log(java.util.logging.level.severe, null, ex); } grab (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(my.class.getname()).log(java.util.logging.level.severe, null, ex); } java.awt.eventqueue.invokelater(new runnable() { public void run() { new my().setvisible(true); } }); } private javax.swing.jbutton jbutton1; private javax.swing.jpanel jpanel1; private javax.swing.jscrollpane jscrollpane1; private javax.swing.jtable jtable1; private javax.swing.jtextfield jtextfield1; }
so tested out hard coded values (which should always utilize mcve need database access), , problem lies here
jtextfield1.settext(" ");
when first focus text field, there space before caret. it's hard notice, it's there. causes regex not match, because " hello"
not match "hello"
.
when pressed button, explicitly set text ""
(no space), that's why works. need set text of text field nil (not space).
java swing
No comments:
Post a Comment