Question

I'm trying to present messages in a JTextArea whenever a method's tasks are done. My methods take some time. This is about connecting to the network or using I/O, you already know these tasks take time.

To make the application more user-friendly, I want to show messages in a JTextArea. I've been stuck on this issue since yesterday. I asked a question about this this morning, and got some advice on how to achieve my goal. It appears that SwingWorker is helpful for performing background tasks while continuing to update the GUI.

So I've tried to use this class, seen many examples and Java documents, but I really don't know how to implement this in my application. To help you understand, I made the example below:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class MyMethods {

// Let's suppose this task takes a long time.
public static String firstTask(){
    return "\nfirst task work done.";
}

// Let's suppose this task takes a long time.
public static String secondTask(){
    return "\nsecond task work done.";
}
}


class TaskCounter extends SwingWorker<Integer, Integer> {

public TaskCounter(){}

protected Integer doInBackground(){
    MyMethods.firstTask();
    MyMethods.secondTask();
    return null;
}

protected void done(){
    MyFrame.textArea.append(MyMethods.firstTask());
    MyFrame.textArea.append(MyMethods.secondTask());
}

}

class MyFrame extends JFrame implements ActionListener {

static JPanel panel;
static JTextArea textArea;
static JButton startButton;
TaskCounter task = new TaskCounter();

public MyFrame(){
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel();
textArea = new JTextArea(20,20);
panel.setLayout(new GridLayout(2,2));
startButton = new JButton("START");

panel.add(textArea);
panel.add(startButton);
startButton.addActionListener(this);
add(panel);

setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {    
    task.execute();
}

}

public class SwingWorkerTest {
public static void main(String args[]) {
    MyFrame frame = new MyFrame();

}
}

What I want to do is show messages, not at once, but one by one. When firstTask() is finished, show the message "first work done", and then some time later, when secondTask() finishes, show the message "second work done". Please assume that the firstTask() and secondTask() methods take some time to complete.

I've learend that long-running tasks should be put into thedoInBackground() method, and methods related to updating the GUI should be put into the done() method.

How can I solve this problem?

Thanks for reading, and sorry for awkward expression.

Was it helpful?

Solution

your background threads should call method

String message="hi im processx";
SwingWorker.publish(message);

and you have to ovveride method

SwingWokrer.process().

this method is called on EDT when it is free with the message which was passed before to publish (might be one or more) in the argument.

You should display your progress there

Yo can read more about those methods in SwingWorker documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top