java - cannot send string from one frame to another -
i have been trying create chat program end end message transmission between frames in computer. somehow not working. no error or warning. have been scratching head on 2 hours. user interface , other components working , couldn't find answer in internet. there client script.
import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; class chatclient extends jframe implements actionlistener,focuslistener{ jbutton button; jlabel label; jtextfield text; socket socket; string hostname = "localhost"; final int portno = 3000; printwriter out; bufferedreader bin; int y=10; string defaultmessage = "enter message.."; public chatclient(){ try{ makeui(); socket = new socket(hostname,portno); out = new printwriter(socket.getoutputstream()); bin = new bufferedreader(new inputstreamreader(socket.getinputstream())); new clientthread().start(); }catch(exception ae){ system.out.println("error! --> "+ae.getmessage()); } } public void makeui(){ settitle("firefly-client"); text = new jtextfield(defaultmessage); text.setbounds(10,620,295,40); text.addfocuslistener(this); add(text); button = new jbutton("send"); button.setbounds(310,620,80,40); button.setforeground(color.white); button.setbackground(color.decode("#11a458")); button.setfocuspainted(false); button.addactionlistener(this); add(button); setsize(400,700); setlayout(null); setvisible(true); setdefaultcloseoperation(jframe.exit_on_close); } public void focusgained(focusevent ae){ if(text.gettext().equals(defaultmessage)){ text.settext(""); } } public void focuslost(focusevent ae){ if(text.gettext().isempty()){ text.settext(defaultmessage); } } public void actionperformed(actionevent ae){ if(!text.gettext().isempty()){ if(!text.gettext().equals(defaultmessage)){ out.println(text.gettext()); label = new jlabel(text.gettext()); label.setbounds(10,y,380,20); y = y+20; add(label); revalidate(); repaint(); } } } public static void main(string []args){ try{ new chatclient(); }catch(exception ae){ system.out.println("error! --> "+ae.getmessage()); } } class clientthread extends thread{ public void run(){ string receive; try{ while(true){ receive = bin.readline(); if(!receive.isempty()){ system.out.println(receive); label = new jlabel(receive); label.setbounds(10,y,380,20); y = y+20; label.sethorizontalalignment(swingconstants.right); add(label); revalidate(); repaint(); } } }catch(exception ae){ ae.printstacktrace(); } } } }
and server part of program looks like,
import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; class chatserver extends jframe implements actionlistener,focuslistener{ jbutton button; jlabel label; jtextfield text; socket socket; serversocket ss; string hostname = "localhost"; final int portno = 3000; printwriter out; bufferedreader bin; int y=10; string defaultmessage = "enter message.."; public chatserver(){ try{ makeui(); ss = new serversocket(portno); socket = ss.accept(); out = new printwriter(socket.getoutputstream()); bin = new bufferedreader(new inputstreamreader(socket.getinputstream())); new serverthread().start(); }catch(exception ae){ system.out.println("error! --> "+ae.getmessage()); } } public void makeui(){ settitle("firefly-server"); text = new jtextfield(defaultmessage); text.setbounds(10,620,295,40); text.addfocuslistener(this); add(text); button = new jbutton("send"); button.setbounds(310,620,80,40); button.setforeground(color.white); button.setbackground(color.decode("#11a458")); button.setfocuspainted(false); button.addactionlistener(this); add(button); setsize(400,700); setlayout(null); setvisible(true); setdefaultcloseoperation(jframe.exit_on_close); } public void focusgained(focusevent ae){ if(text.gettext().equals(defaultmessage)){ text.settext(""); } } public void focuslost(focusevent ae){ if(text.gettext().isempty()){ text.settext(defaultmessage); } } public void actionperformed(actionevent ae){ if(!text.gettext().isempty()){ if(!text.gettext().equals(defaultmessage)){ system.out.println(text.gettext()); out.println(text.gettext()); label = new jlabel(text.gettext()); label.setbounds(10,y,380,20); y = y+20; add(label); revalidate(); repaint(); } } } public static void main(string []args){ try{ new chatserver(); }catch(exception ae){ system.out.println("error! --> "+ae.getmessage()); } } class serverthread extends thread{ public void run(){ string receive; try{ while(true){ receive = bin.readline(); if(!receive.isempty()){ system.out.println(receive); label = new jlabel(receive); label.setbounds(10,y,380,20); y = y+20; label.sethorizontalalignment(swingconstants.right); add(label); revalidate(); repaint(); } } }catch(exception ae){ ae.printstacktrace(); } } } }
can tell me what's wrong it?
you need flush output stream after calling println, else sits in output stream buffer , never sent.
out.println(text.gettext()); out.flush(); // add
add flush both programs.
a client gui like:
import java.awt.borderlayout; import java.awt.event.actionevent; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import java.io.bufferedreader; import java.io.printwriter; import java.io.ioexception; import java.io.inputstreamreader; import java.net.socket; import java.util.list; import java.util.concurrent.executionexception; import javax.swing.*; @suppresswarnings("serial") public class chatclient2 extends jpanel { private static final int vis_row_cnt = 25; private static final string host_name = "localhost"; private static final int port_no = 3000; private defaultlistmodel<string> listmodel = new defaultlistmodel<>(); private jlist<string> jlist = new jlist<>(listmodel); private sendaction sendaction = new sendaction("send"); private jbutton sendbutton = new jbutton(sendaction); private jtextfield textfield = new jtextfield(20); private printwriter out; public chatclient2(socket socket) throws ioexception { out = new printwriter(socket.getoutputstream()); socketworker worker = new socketworker(socket); worker.addpropertychangelistener(new workerlistener()); worker.execute(); jlist.setprototypecellvalue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); jlist.setvisiblerowcount(vis_row_cnt); jscrollpane scrollpane = new jscrollpane(jlist); scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always); textfield.setaction(sendaction); jpanel bottompanel = new jpanel(); bottompanel.setlayout(new boxlayout(bottompanel, boxlayout.line_axis)); bottompanel.add(textfield); bottompanel.add(sendbutton); setlayout(new borderlayout()); add(scrollpane); add(bottompanel, borderlayout.page_end); } public void linetogui(string line) { listmodel.addelement(line); } private class sendaction extends abstractaction { public sendaction(string name) { super(name); int mnemonic = (int) name.charat(0); putvalue(mnemonic_key, mnemonic); } @override public void actionperformed(actionevent e) { string text = textfield.gettext(); if (!text.trim().isempty()) { out.println(text); out.flush(); linetogui("me: " + text); } textfield.selectall(); textfield.requestfocusinwindow(); } } private class socketworker extends swingworker<void, string> { private bufferedreader bin; public socketworker(socket socket) throws ioexception { inputstreamreader isr = new inputstreamreader(socket.getinputstream()); bin = new bufferedreader(isr); } @override protected void doinbackground() throws exception { string line = null; while ((line = bin.readline()) != null) { publish(line); } return null; } @override protected void process(list<string> chunks) { (string line : chunks) { linetogui("server: " + line); } } } private class workerlistener implements propertychangelistener { @override public void propertychange(propertychangeevent evt) { if (evt.getnewvalue() == swingworker.statevalue.done) { @suppresswarnings("rawtypes") swingworker worker = (swingworker) evt.getsource(); try { worker.get(); } catch (interruptedexception | executionexception e) { e.printstacktrace(); } } } } public static void main(string[] args) { try { socket socket = new socket(host_name, port_no); swingutilities.invokelater(() -> createandshowgui(socket)); } catch (ioexception e) { e.printstacktrace(); } } private static void createandshowgui(socket socket) { chatclient2 mainpanel = null; try { mainpanel = new chatclient2(socket); } catch (ioexception e) { e.printstacktrace(); system.exit(-1); } jframe frame = new jframe("chatclient2"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(mainpanel); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }
Comments
Post a Comment