Button click blocks GUI - optimization needed

I have a Swing GUI where I redirect thestdin andstderr to aJTextArea by the following

JTextArea textarea_output =new JTextArea(2,20);

PrintStream ps =new PrintStream(new FilteredStream(new ByteArrayOutputStream()));

System.setOut(ps);

System.setErr(ps);

// and an inner class which outputs information within the JTextArea

class FilteredStreamextends FilterOutputStream{

public FilteredStream(OutputStream aStream){

super(aStream);

}

publicvoid write(byte b[])throws IOException{

String s =new String(b);

textarea_output.append(s);

}

Upon a JButton click on the GUI I call within theactionPerformed method an external Java class that does a lot of processing and produces lots of output.

The problem is that the button click blocks everything until the called external Java class finished its processing. The real trouble is that the user has to wait a long time before anything happens and even a single line of output is displayed in theJTextArea...

Any ideas/ suggestions how to optimize/ redesign this in order to get a prompt response, please?

[1794 byte] By [pifprojecta] at [2007-11-27 5:42:10]
# 1
> an external Java class that does a lot of processing and produces lots of output.Then you need to create a separate Thread for that so the GUI event thread isn't blocked. http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
camickra at 2007-7-12 15:20:29 > top of Java-index,Desktop,Core GUI APIs...
# 2

If you are using Java 6, then you might want to have a look at the [url=http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html]SwingWorker[/url] class. This was created specifically for using background threads within the Swing GUI. A decent article on this can be found [url=http://java.sun.com/developer/technicalArticles/javase/swingworker/]here[/url].

Good luck!

/Pete

petes1234a at 2007-7-12 15:20:29 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thank you two very much for pointing me the right way.

From the articles:

Swing applications have a single Event Dispatch Thread for the UI. Any tasks running on the EDT should finish quickly so that your UI is responsive to user input. Performing long-running tasks on the EDT will cause your application to become unresponsive because GUI events will accumulate in the event dispatch queue.

pifprojecta at 2007-7-12 15:20:29 > top of Java-index,Desktop,Core GUI APIs...