java - Update JLabel repeatedly with results of long running task -
i'm writing program pings server. wrote code check once , put ping in jlabel
, put in method called setping()
.
here code
private void formwindowopened(java.awt.event.windowevent evt) { setping(); }
that worked did once, did:
private void formwindowopened(java.awt.event.windowevent evt) { for(;;){ setping(); } }
but doesn't work first time.
i didnt put setping method because long here is:
public string setping(){ runtime runtime = runtime.getruntime(); try{ process process = runtime.exec("ping lol.garena.com"); inputstream = process.getinputstream(); inputstreamreader isr = new inputstreamreader(is); bufferedreader br = new bufferedreader(isr); string line; while ((line = br.readline()) != null) { int = 0; = line.indexof("average"); if(i > 0){ string finalping = ""; line.tochararray(); try { finalping = ""; for(int x = i; x < + 17; x++) { finalping = finalping + (line.charat(x)); } }catch(indexoutofboundsexception e) { try { finalping = ""; for(int x = i; x < + 16; x++) { finalping = finalping + (line.charat(x)); } }catch(indexoutofboundsexception f) { try { finalping = ""; for(int x = i; x < + 15; x++) { finalping = finalping + (line.charat(x)); } }catch(indexoutofboundsexception g){} } } string final1ping = finalping.replaceall("[^0-9]", ""); return final1ping; } } }catch(ioexception e){ } return ""; }
update in case important, im using netbeans. created form , put code in formwindowopened evt instead of calling in main:
private void formwindowopened(java.awt.event.windowevent evt) { actionlistener timerlistener = new actionlistener() { @override public void actionperformed(actionevent e) { new pingworker().execute(); } }; timer timer = new timer(1000, timerlistener); timer.start(); jlabel1.settext(label.gettext()); timer.stop(); // todo add handling code here: } class pingworker extends swingworker { int time; @override protected object doinbackground() throws exception { time = pingtime("lol.garena.com"); return new integer(time); } @override protected void done() { label.settext("" + time); } }; public jcomponent getui() { return label; } public static int pingtime(string hostnameorip) { socket socket = null; long start = system.currenttimemillis(); try { socket = new socket(hostnameorip, 80); } catch (ioexception ex) { ex.printstacktrace(); } { if (socket != null) { try { socket.close(); } catch (ioexception e) { } } } long end = system.currenttimemillis(); return (int) (end - start); }
use swing timer
repeating tasks & swingworker
long running tasks. e.g. of both below - uses timer
repeatedly perform 'long running' task (a ping) in swingworker
.
see concurrency in swing more details on event dispatch thread , doing long running or repeating tasks in gui.
this code combines long running task ('pinging' server) using swingworker
invoked repeating task (updating jlabel
repeatedly times) using swing based timer
.
import java.awt.event.*; import javax.swing.*; import java.net.socket; public class labelupdateusingtimer { static string hostnameorip = "stackoverflow.com"; int delay = 5000; jlabel label = new jlabel("0000"); labelupdateusingtimer() { label.setfont(label.getfont().derivefont(120f)); actionlistener timerlistener = new actionlistener() { @override public void actionperformed(actionevent e) { new pingworker().execute(); } }; timer timer = new timer(delay, timerlistener); timer.start(); joptionpane.showmessagedialog( null, label, hostnameorip, joptionpane.information_message); timer.stop(); } class pingworker extends swingworker { int time; @override protected object doinbackground() throws exception { time = pingtime(); return new integer(time); } @override protected void done() { label.settext("" + time); } }; public static int pingtime() { socket socket = null; long start = system.currenttimemillis(); try { socket = new socket(hostnameorip, 80); } catch (exception wetried) { } { if (socket != null) { try { socket.close(); } catch (exception wetried) {} } } long end = system.currenttimemillis(); return (int) (end - start); } public static void main(string[] args) { runnable r = new runnable() { @override public void run() { new labelupdateusingtimer(); } }; swingutilities.invokelater(r); } }
Comments
Post a Comment