java - JTextArea in other JFrame display realtime console output -
i have "consoleframe" should display console output in real-time jtextarea.
i redirected output streams:
private void redirectsystemstreams() { outputstream out = new outputstream() { @override public void write(int b) throws ioexception { updatetextarea(string.valueof((char) b)); } @override public void write(byte[] b, int off, int len) throws ioexception { updatetextarea(new string(b, off, len)); } @override public void write(byte[] b) throws ioexception { write(b, 0, b.length); } }; system.setout(new printstream(out, true)); system.seterr(new printstream(out, true)); } and call swingutilities.invokeandwait method append new text, works fine
private void updatetextarea(final string text) { try { swingutilities.invokeandwait(new runnable() { @override public void run() { txt_console.append(text); } }); } catch (interruptedexception ex) { } catch (invocationtargetexception ex) { } } but shows me in new consoleframe error: java.lang.error: cannot call invokeandwait event dispatcher thread , because of edt - why work , how can adapt code make work properly?
invokeandwaitmust called out of edt, otherwise caused a.m. exceptions,carefully
invokeandwait, because can freeze whole swing gui, locked exceptionsrepaintmanager(not in cases gui created, relayout, refreshed of methods), applications required restart,for
invokeandwaitrequired testif (eventqueue.isdispatchthread()) {/if (swingutilities.iseventdispatchthread()) {on true cansettext("")/append("") without side effects, output done on edt, practicies wrap insideinvokelateruse
swingworker, there implemented methodsprocess,publish,setprocess,done, mentioned methods notifiededt default,swingworkerdesignated run once time, repeatly (on period) useexecutorswingworkerorrunnable#threadsimple, clear , without side issues, effects
Comments
Post a Comment