Java Swing JTextfield resets after every 2 seconds -


currently i'm programming chat , noticed whenever write in jtextfield without sending server current contents of jtextfield resets "". in code string parts in german shouldnt big problem hope.

first here screenshot of empty jtextfield: write in jtextfield: enter image description here , after test text "hg.." gets deleted after 2-4 seconds without pressing anything. enter image description here

public class clientwindow extends jframe implements runnable{     private static final long serialversionuid = 7413464033743652908l;      private jpanel contentpane;      private jmenubar bar;     private jmenu datei;     private jmenu option;     private jmenu features;      private jmenuitem verlaufspeichern;     private jmenuitem w;     private jmenuitem ww;      private jtextarea history;     private jtextfield text;     private jbutton send;      private defaultcaret caret;      private client client;      private thread run, listen;      public clientwindow(string name, string address, int port){         client = new client(name,address,port);          createwindow();         createpanel();         createlookandfeel();         createjmenu();         createinhalt();         console("versuche zu verbinden.(username: "+ name +", ip: "+ address +", port: "+ port +")");          boolean connect = client.openconnection(address);          if(!connect){             console("verbindung fehlgeschlagen.");         }         string connection = "/c/" + name;         client.send(connection.getbytes());         run = new thread(this,"running");         run.start();     }      public void run(){         listen();     }      public void listen(){         listen = new thread("listen") {             public void run(){                 while(true){                     string message = client.receive();                     if(message.startswith("/c/")){                         client.setid(integer.parseint(message.split("/c/|/e/")[1]));                         console("erfolgreich verbunden.");                     } else if(message.startswith("/m/")){                         system.out.println(message);                         string text = message.substring(3);                         text = text.split("/e/")[0];                         console(text);                     } else if(message.startswith("/i/")){                         string text = "/i/" + client.getid() + "/e/";                         send(text, false);                     }                 }             }         };         listen.start();     }      public void createlookandfeel(){         try {             uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());         } catch (classnotfoundexception | instantiationexception | illegalaccessexception                 | unsupportedlookandfeelexception e) {             e.printstacktrace();         }     }      public void createjmenu(){         bar = new jmenubar();         datei = new jmenu(" datei ");         option = new jmenu(" options ");         features = new jmenu(" features ");         verlaufspeichern = new jmenuitem(" verlauf speichern ");         w = new jmenuitem(" coming ");         ww = new jmenuitem(" coming ");          datei.add(verlaufspeichern);         option.add(w);         features.add(ww);          bar.add(datei);         bar.add(option);         bar.add(features);          setjmenubar(bar);     }      public void createwindow(){         setresizable(false);         settitle("chat - "+ client.getname());         setdefaultcloseoperation(jframe.exit_on_close);         setsize(800,550);         setlocationrelativeto(null);         setvisible(true);     }      public void createpanel(){         contentpane = new jpanel();         contentpane.setborder(new emptyborder(5,5,5,5));         setcontentpane(contentpane);         contentpane.setlayout(null);     }      public void createinhalt(){         history = new jtextarea();         jscrollpane scrollhistory = new jscrollpane(history);          scrollhistory.setbounds(10,10,775,450);         history.seteditable(false);         caret = (defaultcaret) history.getcaret();         caret.setupdatepolicy(defaultcaret.always_update);         contentpane.add(scrollhistory);          text = new jtextfield();         text.setbounds(10, 465, 700, 30);         text.addkeylistener(new keyadapter(){             public void keypressed(keyevent e){                 if(e.getkeycode() == keyevent.vk_enter){                     send(text.gettext(),true);                 }             }         });         contentpane.add(text);          send = new jbutton("senden");         send.setbounds(715, 465, 70, 30);         send.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e) {                 send(text.gettext(),true);             }         });         contentpane.add(send);          addwindowlistener(new windowadapter(){             public void windowclosing(windowevent e){                 string disconnect = "/d/"+ client.getid()+"/e/";                 send(disconnect,false);                 client.close();             }         });          text.requestfocusinwindow();     }      public void send(string message, boolean textb){         if(message.equals("")) return;         if(textb){             message = client.getname() + ": " + message;             message = "/m/"+message;         }         client.send(message.getbytes());         text.settext("");         text.requestfocusinwindow();     }      public void console(string message){         history.append(message + "\n\r");         history.setcaretposition(history.getdocument().getlength());     } } 

in method send have text set "" in line:

text.settext(""); 

here method have mentioned:

public void send(string message, boolean textb){ if(message.equals("")) return; if(textb){     message = client.getname() + ": " + message;     message = "/m/"+message; } client.send(message.getbytes()); text.settext(""); text.requestfocusinwindow(); 

}`

i guess if remove line text not reset , solve problem.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -