I am a novice-java programmer and currently I am developing a graphical swing interface (GUI.java) for a console-based algorithm (algorithm.java).

In my graphical interface, when I click on the "run algorithm" button, the algorithm is executed and the results which previously were written in the console, now they are written in a text area inside the GUI. The problem is that until the computation is not finished (all results are printed) the textarea is not updated, and the GUI gets irresponsible.

I know I have to do it with a Swing Worker but in all the examples that I found the swing workers run on the same class and therefore is easier

This is a part of code (algorithm.java) which displays the printed results of the algorithm into the textarea:

If the code above is in the SwingWorker's doInBackground method, then it suggests that you're trying to update a Swing component directly from the background thread, which is not what you want to be doing. Are you using the SwingWorker's publish/process methods? If not you should as these would allow you to do what you want easily. The Sun tutorial on this can show you the way.

You also look to be doing things here in a static and not OOPs way. If so, you may wish to correct this. Best of luck!

03-12-2010, 03:24 PM

cotarelo

Could you illustrate me with an example?

Thank you in advance

03-12-2010, 03:28 PM

Fubarable

Again, the Sun tutorials have explanation and examples. Have you Googled for this and looked at them?

I have read the tutorials, my problems is that I created the application with the netbeans assistant and there is no such method createandshowgui() in the gui;besides the operation to do in background is a different algorithm in another package and different class. so I dont know where to put the do in background method.

While I do think that it's fine to use NetBeans as an IDE, at this stage you should not be using NetBeans to generate your GUI code. Rather you should be learning to create it yourself by hand since if you did, I think that you'd understand the code that I posted.

03-18-2010, 06:30 PM

cotarelo

I understand the code you posted, but realize swingworker in my case is more complicated.

Please, I would like the same example, but without the swingworker, so I can see how and where you added the different methods and where and how the concurrency works.

ignore what i said about netbeans.

03-18-2010, 07:11 PM

Fubarable

Quote:

Originally Posted by cotarelo

I understand the code you posted, but realize swingworker in my case is more complicated.

Please, I would like the same example, but without the swingworker, so I can see how and where you added the different methods and where and how the concurrency works.

I'm sorry, but I'm still not getting you. To see my code without the SwingWorker, simply get rid of the MySwingWorker class and comment out the use of this class in the action listener:

I still think that for us to help you better you need to write out a well-formed paragraph about just what's not working for you. Also look into creating and posting an SSCCE or "Short, Self Contained, Correct (Compilable), Example" so we can see a small portion of your code that well illustrates your current problem.

03-19-2010, 03:56 PM

cotarelo

Another try:

I have graphical swing interface (GUI.java) for a console-based algorithm (algorithm.java). Inside the constructor of algorithm.java there is a method execute() which do some computations and prints them into a text area.

In my graphical interface, when I click on the "run algorithm" button, the algorithm.java is executed. The problem is that until the computation is finished the GUI gets irresponsible.

For some reason, in the Doinbackground method, I receive the "From Swing worker" text on the textarea, but the call to the algorithm is never done, and I do not receive the results of the computations in the textarea. Of course without the swingworker the call to the algorithm works.

03-22-2010, 03:26 PM

Fubarable

Quote:

Originally Posted by cotarelo

Thanks for the reply, my code was very messy and I have some mistakes. Now I am getting closer, as the multi threading seems to work, but, still I can not do what I want.

This is my code in GUI.java
For some reason, in the Doinbackground method, I receive the "From Swing worker" text on the textarea, but the call to the algorithm is never done, and I do not receive the results of the computations in the textarea. Of course without the swingworker the call to the algorithm works.

I have requested several times for an SSCCE. If you are serious about needing help, then it would be in your best interest to make the effort to read the link about this, create one, and post it. Otherwise I only have partial information, and certainly not enough to be able offer advice. Up to you.

Best of luck.

03-22-2010, 05:51 PM

cotarelo

My only problem is that this call inside the Doinbackgound method.

Code:

Algorithm algo = new Algorithm(arrayArgum, outputpath, view);

Seems to never be done. The algorithm should answer in my textarea with results (if I place the call outside the the swingworker method it does it correctly).

However, the text From Swing Worker: 1
From Swing Worker: 2.
...

It does it correcly, and I can move the scroll bar in the textarea when it is still running

03-22-2010, 10:29 PM

Fubarable

Quote:

Originally Posted by cotarelo

My only problem is that this call inside the Doinbackgound method.

Code:

Algorithm algo = new Algorithm(arrayArgum, outputpath, view);

Seems to never be done. The algorithm should answer in my textarea with results (if I place the call outside the the swingworker method it does it correctly).

However, the text From Swing Worker: 1
From Swing Worker: 2.
...

It does it correcly, and I can move the scroll bar in the textarea when it is still running

Isolate the problem by creating an SSCCE. Seriously, it works.

03-23-2010, 10:05 AM

cotarelo

The view was redeclared

Code:

GUI view;

And that is why it was not passing the parameters.
PS: I tried to do a SSCCE but I did not know how, the page is very unclear, and sure an poster does not want to loose time on this. maybe a tutorial in this forum would work

03-23-2010, 12:29 PM

Fubarable

Quote:

Originally Posted by cotarelo

The view was redeclared

Code:

GUI view;

And that is why it was not passing the parameters.

and can you see why there's no way we can see that this was your problem given what you've posted above? An SSCCE would have shown this to us days ago.

Quote:

PS: I tried to do a SSCCE but I did not know how, the page is very unclear, and sure an poster does not want to loose time on this. maybe a tutorial in this forum would work

It's a pretty straight forward process of trying to isolate your problem into a small compilable program that we can run and view. Why don't you let us know what parts about the page are confusing and we can help you with it. Otherwise we will all continue to be frustrated by your posting snippets of code that don't contain the actual problem, and that we can't run or test. Thus if you had posted a proper SSCCE in the first place, you'd likely have had your solution days ago and would have saved time,... but it's your dollar.