aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Button stays pressed while i create data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Button stays pressed while i create data " Watch "Button stays pressed while i create data " New topic
Author

Button stays pressed while i create data

Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Hi there i have a save button that when i save data to a file it stays pressed.It is implemented in Swing.

here is the code


how can i make the button be released immediately and not stay pressed while i save to a file.If i save 200000 data it stays a while pressed.

I thought creating a Thread on the Method that i create the data but how can i do it ?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Yes, that's exactly right. But unfortunately it's a bit more complicated than just running the update in a separate thread. So read the tutorial: Lesson: Concurrency in Swing.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1823
    
    7

Don't perform the save operation on the Event Dispatch Thread, but use a SwingWorker to spin-off a new worker thread to perform the save operation instead. Have a look at this tutorial.

Edit: Aaaaaw, I almost made it...


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
i will read them and tell you about thanks for the moment.
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Paul Clapham wrote:Yes, that's exactly right. But unfortunately it's a bit more complicated than just running the update in a separate thread. So read the tutorial: Lesson: Concurrency in Swing.


So i have read it and if i have understand that i must create a swingworker extends to my class where i produce the data !Is that Right?But my question is that class is my model.Is it a good practice to do it there or should i create an other class that will do the job.I mean a class which extends the swingwkorker and there to put all the job that i want to be done in backround? And then send them to model for persistence?

can you help me ont his?
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Jelle Klap wrote:Don't perform the save operation on the Event Dispatch Thread, but use a SwingWorker to spin-off a new worker thread to perform the save operation instead. Have a look at this tutorial.

Edit: Aaaaaw, I almost made it...

So i have read it and if i have understand that i must create a swingworker extends to my class where i produce the data !Is that Right?But my question is that class is my model.Is it a good practice to do it there or should i create an other class that will do the job.I mean a class which extends the swingwkorker and there to put all the job that i want to be done in backround? And then send them to model for persistence?

can you help me ont his?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

I suggest that you imitate what you read in the tutorial. There were examples, weren't there? And did they extend SwingWorker, or did they do something else?
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Paul Clapham wrote:I suggest that you imitate what you read in the tutorial. There were examples, weren't there? And did they extend SwingWorker, or did they do something else?


I have create the Worker class just fine the button don't stays pressed .It's easier that i thought or i have done something wrong?
The only error i get is when i add the

worker.addPropertyChangeListener(this); it says to create the method only when i put null inside worker.addPropertyChangeListener(null); i don't get the error.
this is what i get does anyone know why ? The button it's ok now but i dont get the results .

method addPropertyChangeListener in class javax.swing.SwingWorker<T,V> cannot be applied to given types;
required: java.beans.PropertyChangeListener
found: controller.Controller.generateListener
reason: actual argument controller.Controller.generateListener cannot be converted to java.beans.PropertyChangeListener by method invocation conversion
----

what i also get is this
error: method addPropertyChangeListener in class SwingWorker<T,V> cannot be applied to given types;
worker.addPropertyChangeListener(this);
required: PropertyChangeListener
found: Controller.generateListener
reason: actual argument Controller.generateListener cannot be converted to PropertyChangeListener by method invocation conversion
where T,V are type-variables:
T extends Object declared in class SwingWorker
V extends Object declared in class SwingWorker

any idea ??
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
The exception is saying the method addPropertyChangeListener takes a parameter that must be assignable to type java.beans.PropertyChangeListener but it has been passed an object of type controller.Controller.generateListener.
In other words the object that 'this' refers to must implement the interface java.beans.PropertyChangeListener.
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Tony Docherty wrote:The exception is saying the method addPropertyChangeListener takes a parameter that must be assignable to type java.beans.PropertyChangeListener but it has been passed an object of type controller.Controller.generateListener.
In other words the object that 'this' refers to must implement the interface java.beans.PropertyChangeListener.


I have fix it i change the actions .I have 10 in a controller and i had create inner class for all now i have one actionlistener for all and it woriks.

But i still dont see a big difference with the swingworker class .The button also stays pressed. Now i have it setFALSE .If i put Threaad .sleep(100); it stays stack the whole GUI and nothing works.
if any help her eis the code

Here is the doInBackRound() Method of thew Swing worker

and here is the code for the action listener



and propertychange



I have also read the progressBAR tutorial but i canot find a way to put the progress bar .If i put the progressbar inside the doInBackround() it fills the progressbar immidietly after the doInBackround() is finished.
If i put a Thread it stays stack and do nothing .What do i do wrong ?Where should i put the progress bar and how so that when i press the button the progress bar is filling together with doInBackground method?.And how can i recreate the swingwoker class so that it can make more things.

Forget to ask how do i know that swingworker worksfine?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

You seem to be using PropertyChangeListener for getting interim results. Instead the right way is to use publish-process. In the tutorial that was suggested to you earlier, there is a page with the title "Tasks that Have Interim Results". Here is the link.
Read through that to understand how to do that. Also, in the subsequent pages, you can find a progress bar example.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Ranganathan Kaliyur Mannar wrote:You seem to be using PropertyChangeListener for getting interim results. Instead the right way is to use publish-process. In the tutorial that was suggested to you earlier, there is a page with the title "Tasks that Have Interim Results". Here is the link.
Read through that to understand how to do that. Also, in the subsequent pages, you can find a progress bar example.


yew i have read sams java 6 and it was written there i will read that and come back
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
I have solve it .The only question that i have is can i use More than one SwingWorker class for a different works?Is it ok or do i have to use something else? .
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

Horche Gratsias wrote:I have solve it .

Cool. so, how did you solve it? did you get rid of PropertyChangeListener and used SwingWorker instead (via publish-process) ? we would like to know.

Horche Gratsias wrote:
The only question that i have is can i use More than one SwingWorker class for a different works?Is it ok or do i have to use something else? .

Sure, I don't see any problems with that, though I have myself never tried it.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

And, I think this discussion fits in our Swing forum (where GUI issues are discussed).
Do you mind if I move it?
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Ranganathan Kaliyur Mannar wrote:And, I think this discussion fits in our Swing forum (where GUI issues are discussed).
Do you mind if I move it?

but it has to do with Threads ?No i don't have any problem about it
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Ranganathan Kaliyur Mannar wrote:
Horche Gratsias wrote:I have solve it .

Cool. so, how did you solve it? did you get rid of PropertyChangeListener and used SwingWorker instead (via publish-process) ? we would like to know.

Horche Gratsias wrote:
The only question that i have is can i use More than one SwingWorker class for a different works?Is it ok or do i have to use something else? .

Sure, I don't see any problems with that, though I have myself never tried it.

what i mean is that i have made it like(MVC) this every logic i have made has a controller but for most of them i have to do backroundworks so i can use more than one swingworker class or is there any other way or should i just use the same ?Ion the doInBackround Method i already imlement something if i want to to something more how can i use this method ?is there any other way or i must use a new swingworker class?thats why i ask you .Thanks
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Ranganathan Kaliyur Mannar wrote:
Horche Gratsias wrote:I have solve it .

Cool. so, how did you solve it? did you get rid of PropertyChangeListener and used SwingWorker instead (via publish-process) ? we would like to know.

Horche Gratsias wrote:
The only question that i have is can i use More than one SwingWorker class for a different works?Is it ok or do i have to use something else? .

Sure, I don't see any problems with that, though I have myself never tried it.


look at this


and i ahve create a Thread in my Controller like this


now everything works no exceptions lo dead locks no freezing Gui no no nothing.I believe it looks good and it's ok


I would like also your opinions about it .

It s all implement it like MVC in Swing i have use Swingworker for this and some Threads.it was difficult thats for sure and i am not sure if it's ok but it works like a charm .

Now only left the progress bar to increase together with the doInBackround method
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

You can surely use more than one SwingWorker if you think there are different types of tasks to be done and it would be easier to handle it separately. You can also do a series of operations in a single SwingWorker in the doInBackground() method. It really depends on the requirement.

Edit: My post co-incided with your new post. You don't need to start the worker itself in a separate EventQueue.invokeLater(). This can be done within your GUI classes (the GUI construction itself should be invoked with SwingUtilities.invokeLater).

Moved this discussion to our Swing forum where you will get more replies.
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Ranganathan Kaliyur Mannar wrote:You can surely use more than one SwingWorker if you think there are different types of tasks to be done and it would be easier to handle it separately. You can also do a series of operations in a single SwingWorker in the doInBackground() method. It really depends on the requirement.

like what can you give an example ?

how do you seee the code above?
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Ranganathan Kaliyur Mannar wrote:You can surely use more than one SwingWorker if you think there are different types of tasks to be done and it would be easier to handle it separately. You can also do a series of operations in a single SwingWorker in the doInBackground() method. It really depends on the requirement.

Edit: My post co-incided with your new post. You don't need to start the worker itself in a separate EventQueue.invokeLater(). This can be done within your GUI classes (the GUI construction itself should be invoked with SwingUtilities.invokeLater).

Moved this discussion to our Swing forum where you will get more replies.

can you explain in details i didn't catch it ?
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Ranganathan Kaliyur Mannar wrote:You can surely use more than one SwingWorker if you think there are different types of tasks to be done and it would be easier to handle it separately. You can also do a series of operations in a single SwingWorker in the doInBackground() method. It really depends on the requirement.

Edit: My post co-incided with your new post. You don't need to start the worker itself in a separate EventQueue.invokeLater(). This can be done within your GUI classes (the GUI construction itself should be invoked with SwingUtilities.invokeLater).

Moved this discussion to our Swing forum where you will get more replies.


look how it starts

Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Ranganathan Kaliyur Mannar wrote:You can surely use more than one SwingWorker if you think there are different types of tasks to be done and it would be easier to handle it separately. You can also do a series of operations in a single SwingWorker in the doInBackground() method. It really depends on the requirement.

Edit: My post co-incided with your new post. You don't need to start the worker itself in a separate EventQueue.invokeLater(). This can be done within your GUI classes (the GUI construction itself should be invoked with SwingUtilities.invokeLater).

Moved this discussion to our Swing forum where you will get more replies.

Yes i have try that you are right.thanks so the solution was to avoid the property listener
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

Horche Gratsias wrote:
like what can you give an example ?
how do you seee the code above?


well, in my doInBackground():

So, I do more than one task and also publish my intermediate results by updating the GUI (in this case appending to a JTextArea).

In your code I see lot of sysout statements. I think you can replace them with publish-process and update the GUI.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

Horche Gratsias wrote:Yes i have try that you are right.thanks so the solution was to avoid the property listener

PropertyListener is very useful, but in Swing if you don't want to block the GUI with long operations, you better use SwingWorker and the publish-process model to update the GUI for interim results.
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Ranganathan Kaliyur Mannar wrote:
Horche Gratsias wrote:Yes i have try that you are right.thanks so the solution was to avoid the property listener

PropertyListener is very useful, but in Swing if you don't want to block the GUI with long operations, you better use SwingWorker and the publish-process model to update the GUI for interim results.

Thanks


Ranganathan Kaliyur Mannar wrote:
Horche Gratsias wrote:
like what can you give an example ?
how do you seee the code above?


well, in my doInBackground():

So, I do more than one task and also publish my intermediate results by updating the GUI (in this case appending to a JTextArea).

In your code I see lot of sysout statements. I think you can replace them with publish-process and update the GUI.

Thanks for your answer.

But i don't want that when i enter the doInBackround() to start the tasks dotask()1; nad doTask()2; at the same time.If i want the doTask2 to start another time ?what can i do for that?or even first the doTask()2; and then the doTasks()1; ?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

Horche Gratsias wrote:If i want the doTask@ to start another time ?what can i do for that?

yah, create another SwingWorker. Note that, just the creation of SwingWorker doesn't do anything. You have to 'execute' the worker. So, you can control when you want to call the execute on the corresponding worker.
 
 
subject: Button stays pressed while i create data