Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to do GUI input processing after firing off SwingWorker?  RSS feed

 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a previous question I asked how I could implement processor-intensive work, which was called by a Swing component, without the GUI locking up. I was then pointed to the SwingWorker class. I implemented this and it works! But...

I am writing a program that solves sudoku puzzles. It has a Swing GUI with a "solve" button. I have written an actionListener class with an actionPerformed method that is invoked when the user presses this button. It is the following:



The code creates a worker, a new instance of SudokuSolveWorker. The worker.execute() statement causes the doInBackground() method of this class to be called. This solves the Sudoku. A property change listener "picks up" the result:



As I wrote, this works without freezing up the user interface in the sense that the program seems unstable. However, the user interface does not respond to "commands" (mouse clicks) anymore until the worker thread has finished.

In the first code fragment I create an instance of SolveFrame which is an extension of a JFrame. It is a simple frame with a "cancel" button. It is drawn on the screen, even though it is called after the worker.execute() statement. I'd like the user to be able to click this "cancel" button, after which the solving of the sudoku puzzle should be stopped. However, since the program does not respond to mouse clicks anymore, the "cancel" button cannot be pressed.

How can I solve this?
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seriously? I provided you with a good answer to your last question: http://www.coderanch.com/t/640432/GUI/java/repaint-work-combination-paintComponent-Graphics.

You never bothered to respond.

In that posting you were warned about crossposting.

Yet once again you are back at cross posting: http://www.javaprogrammingforums.com/awt-java-swing/40111-swingworker-implemented-how-do-additional-gui-processing.html

So, I shall not waste my time a second time.
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thanks for your answer to my question about the repaint(). I finally understood how it works.

And sorry to annoy you with another question.
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I may have found a clue to an answer. I thought that the propertyChange listener was only invoked when the worker thread ended. However, I just found out that it gets called right after the worker thread is started. And then one of the first lines executed is the worker.get statement. And this is blocking until the worker thread has finished.

That would explain why the GUI becomes unresponsive.

However, what I don't understand then is why the propertyChange listener is invoked before the worker thread ends. According to the docs I have read it should only be invoked then. But apparently my docs are not correct. Who can tell me under what conditions the propertyChange listener is invoked? And how do I make something (a listener or so...) that gets invoked only when the thread has ended?
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Risking the danger that you may have received an answer from one of the other sites:

in your SwigWorker class, do you check for the status 'isCancelled()' or 'isInterrupted()'?

Otherwise: show us some more code, because this swingworker should not affect the working
of your frame in any way. For instance:

You can start as many SwingWorkers as you want, a click on the button will stop
a SwingWorkeer.

Greetz,
Piet
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Evert DeBoer wrote:I think I may have found a clue to an answer.(...)
However, what I don't understand then is why the propertyChange listener is invoked
(...)

I could have saved myself some work here...
But anyway, why not simply print out the propertyevent? Then you see what events are being sent.

Greetz,
Piet
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
Evert DeBoer wrote:I think I may have found a clue to an answer.(...)
However, what I don't understand then is why the propertyChange listener is invoked
(...)

I could have saved myself some work here...


No, on the contrary! You showed me exactly how to stop/cancel a thread, which would have been my next question :-) .

Thanks!


But anyway, why not simply print out the propertyevent? Then you see what events are being sent.


That's a good idea. I will. In the meantime I did entirely without the propertyChange listener and instead used a "done" method in the worker class. That works.

I see you use some Dutch in your examples. Do you happen to have a Dutch site on Java?
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Evert,

yes, using 'done() is what you should use indeed.
But using a PropertyChangeListener is also fine. Just make sure that you check for
the right event. IIRC, the status is sent, including start and Done (see the API for the
exact events), and there are also two members that fire an event, if you want to use
these.

I also have a SudokuSolver, that solves a Sudoku as soon as the user indicates that
the set up is ready. It does this solving on the EDT (I knew too little about threads at
that time), but even the Google expert sudoku's are instantly solved.
There was a thread on this site, in which someone stated a diabolic sudoku, that
took my program 39 sseconds to solve, meanwhile freezing my GUI...

Greetz,
Piet
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Evert DeBoer wrote:(...)
Do you happen to have a Dutch site on Java?

No. I do have a site (in Dutch), but that is about my son's football team.

Greetz,
Piet
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

Ah that's nice for your son :-).

Where can I find the API docs? Do you mean the Oracle site? I haven't been able to find such detailed information there yet.

My sudoku solver now solves sudokus without freezing my GUI. (Since about half an hour :-) .) It uses a sort-of intelligent brute force technique. All solvable sudokus I threw at it were solved within a couple of seconds. However, if the sudoku isn't solvable, it can run for a very long time...

But speed was not the goal of my sudoku solver. I had implemented the same algorithm in Delphi already and that was waaaay faster. But I am learning myself Java, since I want to develop Android apps. And then a Sudoku solver is a nice challenge for a first program.

Groeten!
Mark
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Risking the danger that you may have received an answer from one of the other sites:


The suggestion to use done() was given a while ago: http://www.javaprogrammingforums.com/awt-java-swing/40111-swingworker-implemented-how-do-additional-gui-processing.html#post159331

If the OP had been up front about cross posting, I would have given that answer when I first responded . But it appears the OP still doesn't understand the etiquette on using a forum and doesn't care about wasting our time.

Once again time well spent.
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's simply not true. I eventually found out about the done method myself. And I posted it at both sites.

The reason I posted the question here, too, was that the answers at the other site were going nowhere initially. But if even that is a problem I won't do that in the future.
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet, do you happen to know any good Dutch Java forums? I only know the Tweakers programming forum, which is pretty good, but not Java-specific.
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Evert,

no, I don't. But I must say, I never looked specifically for Dutch forums.

Greetz,
Piet
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!