Tuesday, October 18, 2011

To make the process appear concurrently in Swing java and Flush data to the GUI java

For this you need to us need to use SwingWorker Class which makes the data to be displayed in
Swing Frame as soon as the data is available.


class SwingWorkerDemo extends SwingWorker
//Integer- doInBackground return type and String - publish method Type
{
String command;

public SwingWorkerDemo(String cmd)
{
command=cmd;
area1.setEditable(false);

}
public Integer doInBackground() {
Runtime run = Runtime.getRuntime();
Process pr = null;
try {
pr = run.exec(command);
} catch (IOException ee) {
ee.printStackTrace();
}
int line;
try {
while ((line=pr.getInputStream().read())!=-1) {doInBackground
char c=(char)line;
// System.out.println(Character.toString(c));
publish(Character.toString(c));
}

while ((line=pr.getErrorStream().read())!=-1) {
char c=(char)line;
// System.out.println(Character.toString(c));
publish(Character.toString(c)); /// Publish to the process method to update GUI
}
}
catch (IOException error)
{
System.err.println("Oops");
}
return 0;
}
protected void process(java.util.List chunks) {
for ( String fd: chunks)
{
area1.append(fd); //GUI appending code

}
}
protected void done()
{
JOptionPane.showMessageDialog(null, "Find command Execution Completed.");
}

}

By making use of the SwingWorker class the GUI is updated in the Worker Thread .This makes it
possible for the data to be available in the GUI concurrently when read from the inputStream.

Here the doInBackground process runs the Complex Process and when we publish a data via publish method,the result can be made to be appended to the GUI by the process method.The code in the process methos is executed in the worker Thread so make sure to put the GUI updating code here.

The done method is executed once the process is completed.

Above code is liable to hanging as the process's getErrorStream and getOutputStream is executed in the same thread.So make sure you make the getErrorStream and getOutputStream to be executed in a separate Thread.

No comments:

Post a Comment