java - Application is freezing when database is called -


i have lot of things implemented in componentadapter of java. since loading data database , displaying in jtable, added thread. show 1 method being called such componentadapter

private class displayinitialrevenue_thread implements runnable      {          @override         public void run()          {             displayinitialrevenue_method();         }       }       private void displayinitialrevenue_method()      {         //get dates combo         string selectedcouple = revenueyearcombo.getselecteditem().tostring();          if(selectedcouple.equals("select year"))         {             return;         }          string[] split = selectedcouple.split("/");            //related db          double totalamountinvested;                 //get data database              dbconnector = new dbhandler();              dbconnector.makeconnection();               defaulttablemodel model = (defaulttablemodel) initialrevenuetable.getmodel();              model.setrowcount(0);                 resultset selectalldetails = dbconnector.selectalldetails("sql code here ");               try              {                  if(selectalldetails.isbeforefirst()==false)                  {                      joptionpane.showmessagedialog(null,"this table empty");                  }                  else                  {                      while(selectalldetails.next())                      {                          string clientname = selectalldetails.getstring("client name");                          string providername = selectalldetails.getstring("provider name");                          double amountinvested = selectalldetails.getdouble("invest_amount");                           //get other data                            //update table                          object[]row = {dates,clientname,providername,amountinvested};                           model.addrow(row);                           //get total                          amountinvested = amountinvested+amountinvested;                       }                       //add sum                      object[]blankrow = {null,null,null,null};                      model.addrow(blankrow);                        object[]row = {dates,clientname,providername,amountinvested};                  }              }              catch(sqlexception sql)              {                  joptionpane.showmessagedialog(null,sql.getlocalizedmessage());              }      } 

and, above thread can called in 3 ways. itemlistener attached jcombobox, actionlistener attached jmenuand componentlistener.

componentlistener

private class displayinitialrevenue extends componentadapter      {          public void componentshown(componentevent e)           {              formmemorizer = formmemorizer.initial_revenue;              //displayinitialrevenue_method();               displayinitialrevenue_thread t = new displayinitialrevenue_thread();              t.run();          }        } 

itemlistener

private class revenueyearcomboaction implements itemlistener      {          @override         public void itemstatechanged(itemevent e)          {             if(e.getstatechange() == itemevent.selected)             {                 int selection = formmemorizer;                  if(selection==-1)                 {                     return;                 }                 else if(selection==formmemorizer.initial_revenue)                 {                     //displayinitialrevenue_method();                     displayinitialrevenue_thread t = new displayinitialrevenue_thread();                     t.run();                 }         } } 

i have lot of these kind of methods data database , feed jtables , take data gui , save in database.

now question is, of these freezing sometimes, whenever database call occurred. thought bcs of thread issue made above displayinitialrevenue_thread call displayinitialrevenue_method() test. invoked area related call method still freezes sometimes! other database methods not in separate threads, method is, why calling "only" method lead freeze? in thread!

for side note, in java 8, using mysql server version: 5.6.16 - mysql community server (gpl) comes xampp.

call t.start() start new thread, calling thread#run nothing more calls run method of thread within same thread context...

having said that, swing not thread safe, swing requires updates ui made within context of event dispatching thread. instead of using thread, should consider using swingworker, allows execute long running tasks in background thread, provides easy use publish/process methods , calls done when completes, executed within context of edt you.

see worker threads , swingworker more details


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -