swing - java GUI trying to get multiple inputs from multiple text fields -
how multiple inputs multiple textfields have created? want 1 text field port number, file location. once user enter int , string, can use these inputs program.
i new this, when tried implement this, enter port number, , ui seems "freeze" , can't enter file location.
constructor frame
public tcpservercomparecsv () { setlayout(new flowlayout()); // "this" frame sets layout flowlayout, arranges components // left-to-right, , flow next row top-to-bottom. lblport = new label("port"); // construct label add(lblport); // "this" frame adds label tfport = new textfield("0", 10); // construct textfield tfport.seteditable(true); //edit text add(tfport); // "this" frame adds tfcount tfport.addactionlistener(this); // event-handling lbllocation = new label("csv file location"); // construct label add(lbllocation); // "this" frame adds label tflocation = new textfield("text", 40); // construct textfield tflocation.seteditable(true); //edit text add(tflocation); // "this" frame adds tfcount tflocation.addactionlistener(this); settitle("compare"); // "this" frame sets title setsize(250, 100); // "this" frame sets initial window size setvisible(true); // "this" frame shows addwindowlistener(this); // "this" frame fires windowevent registered windowevent listener // "this" frame adds "this" object windowevent listener }
action event
/** actionevent handler - called when user clicks button. */ @override public void actionperformed(actionevent evt) { // string entered textfield tfport, convert int port = integer.parseint(tfport.gettext()); filelocation = tflocation.gettext(); string csvname = filelocation; serversocket serversocket = null; try { serversocket = new serversocket(port); } catch (ioexception e) { system.err.println("could not listen on port: 57635."); system.exit(1); } socket clientsocket = null; system.out.println ("waiting connection....."); try { clientsocket = serversocket.accept(); } catch (ioexception e) { system.err.println("accept failed."); system.exit(1); } system.out.println ("connection successful"); system.out.println ("waiting input....."); printwriter out = null; try { out = new printwriter(clientsocket.getoutputstream(), true); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } bufferedreader in = null; try { in = new bufferedreader( new inputstreamreader( clientsocket.getinputstream())); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } string inputline, outputline; try { while ((inputline = in.readline()) != null) { system.out.println ("server: " + inputline); if (inputline.trim().equals("bye.")) { system.out.println("exit program"); break; } scanner input1 = new scanner(new file(csvname)); scanner input2 = new scanner(new file(csvname)); scanner input3 = new scanner(new file(csvname)); scanner input4 = new scanner(new file(csvname)); string csvline = getcsvlineval (getlocation34csv(gettag34value(tag34location(gettagcsv( parsefixmsg(inputline ,inputline))), getvaluecsv( parsefixmsg(inputline ,inputline))), getval34(input1, input2)), getcsvline( input3, input4) ); outputline = compareclientfixcsv( gettagcsv( parsefixmsg(inputline ,inputline)), getvaluecsv(parsefixmsg(inputline ,inputline)), getcsvtag(csvline), getcsvvalue(csvline)); out.println(outputline); input1.close(); input2.close(); input3.close(); input4.close(); } } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } out.close(); try { in.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { clientsocket.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { serversocket.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } }
by adding actionlistener
both text fields, if remember correctly, fire event hit return in 1 of them. trigger network code immediately.
you should register action button.
moreover, pointed out in comment, executing network communications on gui thread, causing freeze.
in action implementation, must spawn separate thread actual network communications prevent blocking. find out how works, @ documentation runnable
, executor framework.
Comments
Post a Comment