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?

  • invokeandwait must called out of edt, otherwise caused a.m. exceptions,

  • carefully invokeandwait, because can freeze whole swing gui, locked exceptions repaintmanager (not in cases gui created, relayout, refreshed of methods), applications required restart,

  • for invokeandwait required test if (eventqueue.isdispatchthread()) { / if (swingutilities.iseventdispatchthread()) { on true can settext("")/append("") without side effects, output done on edt, practicies wrap inside invokelater

  • use swingworker, there implemented methods process, publish, setprocess , done, mentioned methods notified edt default,

  • swingworker designated run once time, repeatly (on period) use executor swingworker or runnable#thread simple, clear , without side issues, effects


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -