multithreading - Java: TCP connection in the background thread. How to display JDialog? -


i trying send message server via tcp/ip, wait response, , perform further actions dependent on response. while communication in background process, dialog spinning activity indicator should displayed. furthermore, if there error in connection, jdialog should displayed.

this method invokes connection result of pressing button:

public void sendmessage() {    jframe f = new jframe();     jdialog modaldialog = new jdialog(f, "busy", dialog.modalitytype.modeless);    modaldialog.setsize(200, 100);    modaldialog.setlocationrelativeto(f);    modaldialog.setundecorated(true);    // remove menu buttons    modaldialog.getrootpane().setwindowdecorationstyle(jrootpane.none);    modaldialog.getcontentpane().setbackground( color.white );    // add rotating activity indicator    imageicon loading = new imageicon("src/images/activityindicator.gif");    modaldialog.add(new jlabel("please wait... ", loading, jlabel.center));    // set activity indicator visible    modaldialog.setvisible(true);      // create connection object     igaconnectiontcp t = new igaconnectiontcp(makemessagetoserverflightplan());      // start connection     t.start();      synchronized(t)     {         /* create notification dialog */                  try         {             t.wait();         }         catch(interruptedexception e)         {          }           /*  when activity in background thread ends, change gui on             dispatch thread. */         swingutilities.invokelater(() ->         {              t.closeconnection();              // close pop-up menu when finished             modaldialog.setvisible(false);             modaldialog.dispose();          });      } } 

this tcp connection class:

public class igaconnectiontcp extends thread {   /** object appsingleton */   appsingleton appsingleton = appsingleton.getinstance( );   /** terminator: message server ends connection. */   private final string  terminator = "end_igoconnect_data";   /** message sent server. */   private string messagetoserver;   /** instance of socket. */   socket socket;   /** instance of datainputstream. */       datainputstream input;   /** instance of dataoutputstream. */     dataoutputstream output;   /** instance of bufferedreader. */     bufferedreader br;   /** message sent server. */   private string messagefromserver;    public igaconnectiontcp(string messagetoserver)   {     this.messagetoserver = messagetoserver;   }     @override   public void run()   {     synchronized(this)     {         setmessagefromserver(connectclient());         notify();          system.out.println("full data:");         system.out.println(getmessagefromserver());     }   }    /**    * http://www.dreamincode.net/forums/topic/139154-transfer-a-file-over-tcp/    * @return message received server    */   public string connectclient()   {     string data = "";      try     {         // ip address , port         int serverport = appsingleton.getipportshared();         string ip = appsingleton.getipaddressshared();          // create socket, input , output         socket = new socket(ip, serverport);         input = new datainputstream( socket.getinputstream());         output = new dataoutputstream( socket.getoutputstream());          //send message server         output.writebytes(messagetoserver);          // ++++++++++++++++++++++++++++++         //read response         // ++++++++++++++++++++++++++++++          br = new bufferedreader(new inputstreamreader(socket.getinputstream()));          stringbuilder sb = new stringbuilder();         string s = "";          int value = 0;          while((value = br.read()) != -1)          {             // converts int character             char c = (char)value;              sb.append(c);              if(sb.tostring().contains(terminator))             {                 break;             }         }          // return data obtained server         data = sb.tostring();     }      catch (unknownhostexception e)     {         string message = "application not able establish connection.\n"                 + "check whether ip address , port number correctly entered \n"                 + "in settings. check whether connection not being blocked \n"                 + "your firewall.";         joptionpane.showmessagedialog(new jframe(), message, "tcp connection error",         joptionpane.error_message);     }      catch (eofexception e)     {         string message = "application not able establish connection.\n"                 + "check whether ip address , port number correctly entered \n"                 + "in settings. check whether connection not being blocked \n"                 + "your firewall.";         joptionpane.showmessagedialog(new jframe(), message, "tcp connection error",         joptionpane.error_message);     }      catch (ioexception e)     {         string message = "application not able establish connection.\n"                 + "check whether ip address , port number correctly entered \n"                 + "in settings. check whether connection not being blocked \n"                 + "your firewall.";         joptionpane.showmessagedialog(new jframe(), message, "tcp connection error",         joptionpane.error_message);     }           {        // needed??     }      return data;   }      public void closeconnection()   {     try     {         system.out.println("check wether closing done here");          if(socket!=null)         {             socket.close();         }         if(input != null)         {             input.close();         }         if(output != null)         {             output.close();         }         if(br != null)         {             br.close();         }     }     catch (ioexception ex)      {         logger.getlogger(igaconnectiontcp.class.getname()).log(level.severe, null, ex);     }      socket = null;     input = null;     output = null;     br = null;   }    /**    * gets message sent server.    * @return message sent server.    */   public string getmessagefromserver() {     return messagefromserver;   }    /**    * sets message sent server.    * @param messagefromserver message sent server.    */   public void setmessagefromserver(string messagefromserver) {     this.messagefromserver = messagefromserver;   } }  

my problem jdialog not show activity indicator , "please wait..." sign. , when error occurs, jdiagog blank well. know happening because of threading problem. best way address it?

thank you!


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -