java - How to add a Text into a List from another Client or Server? -
i trying program own chat can't text 1 client next. have chat list can't add it. if know how solve or have idea might work, love hear it.
public class sendaction implements actionlistener { private jbutton sendbutton; private jbutton sendbuttonserver; private jtextfield text; private jlabel label; static arraylist<string> textlist = new arraylist<string>(); static arraylist<string> textlistserver = new arraylist<string>(); private string chattext; public sendaction(jbutton sendbutton) { this.sendbutton = sendbutton; this.sendbuttonserver = sendbuttonserver; } public sendaction(jtextfield text) { this.text = text; } public sendaction(jlabel label) { this.label = label; } public void actionperformed(actionevent e) { string action = e.getactioncommand(); if (e.getsource() == p2pchatclient.sendbutton) { if (p2pchatclient.text.gettext().equals("")) { } else { textlist.add("client: " + p2pchatclient.text.gettext()); system.out.println("eingegebene arraylist elements: " + textlist); chattext = string.join(" | ", textlist); p2pchatclient.label.settext(chattext); p2pchatclient.text.settext(null); } } if (e.getsource() == p2pchatserver.sendbuttonserver) { if (p2pchatserver.textserver.gettext().equals("")) { } else { textlistserver.add("server: " + p2pchatserver.textserver.gettext()); system.out.println("eingegebene arraylist elements: " + textlistserver); chattext = string.join(" | ", textlistserver); p2pchatserver.labelserver.settext(chattext); p2pchatserver.textserver.settext(null); } } } }
another class:
public class chatserversocket extends thread { private int port; private p2pchatserver servergui; private datainputstream instream; private dataoutputstream outstream; public chatserversocket(int port, p2pchatserver servergui) { this.port = port; this.servergui = servergui; } public void run() { serversocket server = null; try { server = new serversocket(port); } catch (exception e) { system.out.println("fehler: " + e.tostring()); } socket client; while (true) { try { client = server.accept(); inputstream in = client.getinputstream(); outputstream out = client.getoutputstream(); instream = new datainputstream(in); outstream = new dataoutputstream(out); while (true) { string utf = instream.readutf(); // file file = new file(utf); // if (file.isfile()) { // file.delete(); // } sendaction.textlistserver.add(utf); // servergui.setmessage(utf); } } catch (ioexception ioe) { system.out.println("server fehler: " + ioe.tostring()); } } } public void sendtext(string message) { // p2pchatserver.send(); try { if (message != null) { outstream.writeutf(message); } } catch (ioexception ioe) { system.out.println("fehler beim senden im server: " + ioe.tostring()); } catch (nullpointerexception npe) { system.out.println("fehler beim senden im client: " + npe.tostring()); } } }
and another:
public class chatclientsocket extends thread { private string ip; private int port; private p2pchatclient client; private datainputstream instream; private dataoutputstream outstream; public chatclientsocket(string ip, int port, p2pchatclient client) { this.port = port; this.ip = ip; this.client = client; } public void run() { try { socket clientsocket = new socket(ip, port); inputstream in = clientsocket.getinputstream(); outputstream out = clientsocket.getoutputstream(); instream = new datainputstream(in); outstream = new dataoutputstream(out); while (true) { string utf = instream.readutf(); sendaction.textlist.add(utf); // client.setmessage(utf); } } catch (unknownhostexception uhe) { system.out.println("client fehler: " + uhe.tostring()); } catch (ioexception ioe) { system.out.println("client fehler: " + ioe.tostring()); } } public void sendtext(string message) { try { if (message != null) { outstream.writeutf(message); } } catch (ioexception ioe) { system.out.println("fehler beim senden im client: " + ioe.tostring()); } catch (nullpointerexception npe) { system.out.println("fehler beim senden im client: " + npe.tostring()); } } }
i used textarea. seteditable cant edit , scrollarea make scrollable u know.
textarea.seteditable(false); scrollarea = new jscrollpane(textarea);
Comments
Post a Comment