java - Fix an unresponsive GUI due to JTextArea? -
i designing program has jeditorpane users can enter , compile java code. can run program in new process, , output displayed in jtextarea. accomplish extending jtextarea , adding member:
private outputstream writer = new outputstream() { public void write(int b) { console.this.append(string.valueof((char) b)); } }; i have simple getstream() method returns outputstream wrapped in printwriter, , call system.setout() , system.seterr() printwriter.
now here comes issue: if user compiles program lot of output sent console @ once (e.g., infinite loop of system.out.println() calls), entire gui hangs. have attempted fix using swingworker handle append() calls nothing seems work.
is there way keep gui responsive if massive amounts of text being written jtextarea? i'm assuming part of issue amount of time time taken update gui after append() call. there maybe way delay writing jtextarea small amount user can click button terminate process?
i wonder if problem way you're writing in outputstream. perhaps should write stringbuilder object off of edt, , when "\n" comes along, append string on edt jtextarea.
something this:
// called in background thread public void write(int b) throws ioexception { if (b == '\r') return; if (b == '\n') { final string text = sb.tostring() + "\n"; swingutilities.invokelater(new runnable() { // except queued onto event thread. public void run() { textarea.append(text); } }); sb.setlength(0); return; } sb.append((char) b); }
Comments
Post a Comment