Pergunta

Brief description of UI and setup

1)Plain vanilla frame, with button1 and button2 and textarea1 and textarea2

2)Button1 launches a continuous running task using a swingworker myswingworker1 and the swingworker's process method continually updates textarea1

3)Button2 launches a continuous running task using a swingworker myswingworker2 and the swingworker's process method continually updates textarea2

The continually running task in this case is "adb logcat", this command keeps writing continuous log information to the outstream, that I capture and print to the textarea. this capturing and printing is done inside the swingworker.

Problem

Only one one button seems to do the intended task of printing the output to the textarea at a time, either button1 OR button2, whichever was started first. The UI never freezes though.

As a part of the experiment, I replaced one long running task with say a command "dir" which runs and return immediately, ie I attached "dir" to button2. Now if I run button2, it works and then button1 it works too. If I do the other way round only the long running task started by button1 runs and the task started by button2 seems to wait for the long running task to end, which defeats the whole purpose of using a swingworker.

How do solve this problem? How do I prevent my UI from waiting for one swingworker to finish?

code excerpt

public class multiswing extends javax.swing.JFrame {

public multiswing() {
    initComponents();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    myswingworker1 m1=new myswingworker1(jTextArea1);
    m1.execute();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    myswingworker2 m2=new myswingworker2(jTextArea2);
    m2.execute();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new multiswing().setVisible(true);
        }
    });
}

public class myswingworker1 extends SwingWorker {

JTextArea jTextArea = new JTextArea();
Process proc;
Runtime rt = Runtime.getRuntime();

public myswingworker1(JTextArea jTextArea) {
    this.jTextArea = jTextArea;
}

protected Void doInBackground() throws Exception {

    String line = null;
    rt = Runtime.getRuntime();
    proc = rt.exec("adb logcat");
    InputStream stdin = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(stdin);
    BufferedReader br = new BufferedReader(isr);
    while ((line = br.readLine()) != null) {
        publish(line);
    }

    return null;
}

@Override
protected void process(List<String> chunks) {
    String data = chunks.get(0);
    jTextArea.append(data + "\n");
}

}

public class myswingworker2 extends SwingWorker {

JTextArea jTextArea = new JTextArea();
Process proc;
Runtime rt = Runtime.getRuntime();

public myswingworker2(JTextArea jTextArea) {
    this.jTextArea = jTextArea;
}

@Override
protected Void doInBackground() throws Exception {

    String line = null;
    rt = Runtime.getRuntime();        
    proc = rt.exec("dir");
    InputStream stdin = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(stdin);
    BufferedReader br = new BufferedReader(isr);
    while ((line = br.readLine()) != null) {
        publish(line);
    }

    return null;
}

@Override
protected void process(List<String> chunks) {
    String data = chunks.get(0);
    jTextArea.append(data + "\n");
}

}

Foi útil?
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top