tag:blogger.com,1999:blog-6929422799523574458.post5716898079076355816..comments2023-03-24T11:36:03.468+01:00Comments on Albert Attard: Practical Example of Swing WorkerAnonymoushttp://www.blogger.com/profile/05001967782991767837noreply@blogger.comBlogger23125tag:blogger.com,1999:blog-6929422799523574458.post-18309017743809200022013-12-10T22:59:10.790+01:002013-12-10T22:59:10.790+01:00Have a look at the updated version of this article...Have a look at the updated version of this article at: http://www.javacreed.com/swing-worker-example/Anonymoushttps://www.blogger.com/profile/05001967782991767837noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-1799404727780762852013-11-08T12:49:27.978+01:002013-11-08T12:49:27.978+01:00hi, very interesting, thank you. I tried to follow...hi, very interesting, thank you. I tried to follow the SwingWorker in debug, and i found a curiosity that i didn't understand.. the size of the chunk list in process method is always 1.. why SwingWorker uses a list instead a single object?AndreAgostohttps://www.blogger.com/profile/09400625261790317975noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-23797724523079053632012-11-20T11:19:11.116+01:002012-11-20T11:19:11.116+01:00Hi,
I think this answers your question: http://do...Hi,<br /><br />I think this answers your question: http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html#publish(V...)<br /><br />This is done for a good reason, as otherwise you can flood the event thread with too many requests which can easily be collapsed into one event.<br /><br />Hope this helpsAnonymoushttps://www.blogger.com/profile/05001967782991767837noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-9494550052384836662012-11-19T18:05:52.970+01:002012-11-19T18:05:52.970+01:00Hi Albert,
I have a problem with the publish meth...Hi Albert,<br /><br />I have a problem with the publish method that accumulate all the values in a List and then call only once the process method with the whole bunch of values.<br />Any idea why?<br /><br />Thanks for publishing this post.<br />Best, JCDJCDhttps://www.blogger.com/profile/17372475728538518777noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-16584074312883579342012-07-26T17:57:21.819+02:002012-07-26T17:57:21.819+02:00This is a Great Post! It's going onto my list!...This is a Great Post! It's going onto my list! I've been searching for any answer and found it with your informable interface!<br /><br />Why hadn't i thought about it!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-64211609170135024342011-10-11T08:10:36.065+02:002011-10-11T08:10:36.065+02:00Hi,
Yes. Thus your button should do the followin...Hi,<br /><br />Yes. Thus your button should do the following:<br /><br /> 1) Create an instance of the worker and provide all required parameters<br /> 2) Execute the worker<br /><br /><br />Extra things:<br /> 3) Disable the button programmatically<br /> 4) Enable the button when the task is finished<br /><br />Hope this helps.Anonymoushttps://www.blogger.com/profile/05001967782991767837noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-84934341616958912302011-10-11T05:24:38.963+02:002011-10-11T05:24:38.963+02:00"the scroll has to wait for your task to fini..."the scroll has to wait for your task to finish." - Correct !!! <br />"You can use the SwingWorker to carry out the "long" time tasks for you." - Do you mean to say that i put the task that is performing the data input in the text area in the doInBackground() method? instead of putting the scroll action in it?Akki...https://www.blogger.com/profile/05111962451595748333noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-14176988288019145642011-10-10T13:52:13.435+02:002011-10-10T13:52:13.435+02:00From what I can understand (from your description)...From what I can understand (from your description), the task is being handled by the event thread and the scroll has to wait for your task to finish.<br /><br />You can use the SwingWorker to carry out the "long" time tasks for you. This will prevent your application from hanging.Anonymoushttps://www.blogger.com/profile/05001967782991767837noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-56286091867514782722011-10-10T13:11:19.536+02:002011-10-10T13:11:19.536+02:00Hi Albert,
I have a query.
My application has a J...Hi Albert,<br />I have a query. <br />My application has a JTextArea which displays certain data when a button is clicked on the application. This JTextArea is contained in a JScrollPane. Lets say the JTextArea can accommodate 10 lines before the vertical scroll bar is invoked.<br />What happens is, when i click the button, the data starts coming on the JTextArea, but once the data crosses 10 lines, the scroll bar doesn't get activated until the process invoked by the button press is over. Once that process is over, the scroll bar appears. Until then the data in the text area just gets filled in the invisible region. <br />Lemme know if my explanation is not clear. <br />As an example take this text box i am typing my comment in. Imagine the scroll bar not getting activated until i finish typing the whole comment.<br />That's whats happening. Can i use SwingWorker to resolve this issue? If yes how?<br />Thanks<br />AkshayAkki...https://www.blogger.com/profile/05111962451595748333noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-31768175234798198592010-09-21T13:03:21.806+02:002010-09-21T13:03:21.806+02:00I figured out it has nothing to do with the worker...I figured out it has nothing to do with the worker thread certainly. Its more to do with loading a native dll and exception is coming in that.<br />thanks. I ll try to fix it.<br /><br />Regardsnamannoreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-83593362952977907802010-09-20T10:51:21.717+02:002010-09-20T10:51:21.717+02:00Thanks a lot for that. I ll try out till then, and...Thanks a lot for that. I ll try out till then, and also send you my code. See whenever you get time.<br /><br />Thanks!namannoreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-43917234886725773302010-09-20T09:41:50.913+02:002010-09-20T09:41:50.913+02:00It shouldn't be a problem whether you're u...It shouldn't be a problem whether you're using threads or not. If you want to confirm, use it with out a worker and confirm. But the exception you're getting is related with static contents. May be you're missing some property files.<br /><br />I never worked with that, but if you want you can send me the code at albertattard@gmail.com and I'll have a look (later on this week - as I'm quite busy with other stuff).Anonymoushttps://www.blogger.com/profile/05001967782991767837noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-14070355429117450342010-09-20T08:54:16.422+02:002010-09-20T08:54:16.422+02:00Thanks. I actually declared
CommPortIdentifier x,...Thanks. I actually declared <br />CommPortIdentifier x, it ran without errors.<br /><br />After seeing http://www.control.lth.se/~kurstr/InternDator/java/commapi/javadocs/javax.comm.CommPortIdentifier.html , I realize that the CommPort class has no constructor. The getPortIdentifier is a static method and needs to be called with the class reference.<br /><br />So that is where the static parts are coming in maybe. Your suggestions on this?<br /><br />Could you suggest how do we achieve the task of using the CommPort class in a worker. I need a serial port scanner in my worker.<br /><br />Thanks.namannoreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-7349000092408001342010-09-20T08:36:44.988+02:002010-09-20T08:36:44.988+02:00Declare a variable of type CommPortIdentifier, wit...Declare a variable of type CommPortIdentifier, without instantiating it and see what happens. Note that NetBeans may be providing additional resources which you're missing in the normal execution. Does this class use properties or resources (from files)?Anonymoushttps://www.blogger.com/profile/05001967782991767837noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-48611113333300132742010-09-20T08:34:06.899+02:002010-09-20T08:34:06.899+02:00Okay. Ya i read about that exception means some pr...Okay. Ya i read about that exception means some problem with static parts.<br />But the funny part is it works very well when I debug the code in netbeans by putting breakpoints. It just throws this error when I do I "run project". <br />Additionaly when I remove that instantiation, the error goes off, so does that mean some static initialization happening inside the CommPortIdentifier class ?namannoreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-31522955343548927452010-09-20T08:12:08.517+02:002010-09-20T08:12:08.517+02:00This can be a hard error to find. If my memory ser...This can be a hard error to find. If my memory serves me well, this error occurs when an exception is thrown during static initialisation. It has nothing to do with the instances, there is a problem with the static parts of a particular class.Anonymoushttps://www.blogger.com/profile/05001967782991767837noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-20087322957449370222010-09-20T07:28:52.617+02:002010-09-20T07:28:52.617+02:00Hello Albert,
Thanks for your post. Other things ...Hello Albert,<br /><br />Thanks for your post. Other things work fine but when I try to do this inside the worker <br />portList=CommPortIdentifier.getPortIdentifiers();<br /><br />I get a nasty error-><br />java.util.concurrent.ExecutionException: java.lang.ExceptionInInitializerError<br /><br />Could you please give some pointers about what could be the problem.<br />Thanks.namannoreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-8392775012259635132010-07-20T19:15:31.509+02:002010-07-20T19:15:31.509+02:00I'm not 100% sure about that, but I think yes....I'm not 100% sure about that, but I think yes. The finally block will be invoked before the try/catch/finally block is out scoped.<br /><br />The cancel action cannot force a method out (You cannot pop a method from the stack). That is the doInBackground() method cannot be terminated by an external source until it is complete. You can check from within the doInBackground() method whether the process was cancelled or not and act accordingly.Anonymoushttps://www.blogger.com/profile/05001967782991767837noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-30210302662470679902010-07-20T18:51:52.052+02:002010-07-20T18:51:52.052+02:00Hi, Albert,
Thanks for your post. As for your Sea...Hi, Albert,<br /><br />Thanks for your post. As for your SearchForWordWorker example, if it gets canceled, does it guarantee to close the files it opened? That is, is the finally{} block in doInBackground() guaranteed to run? <br /><br />Thanks!Unknownhttps://www.blogger.com/profile/15005181298766712974noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-20785591506509513492010-04-25T08:04:16.211+02:002010-04-25T08:04:16.211+02:00Hi Mehta,
My Thread.sleep() is there to simulate ...Hi Mehta,<br /><br />My Thread.sleep() is there to simulate a delay. I have a simple example and without it, it would run so fast that you'll see nothing. That's why I added the sleep there.<br /><br />As for your problem; synchronization may be your answer. If you want, email me your code and I will try to have a look. My email is albertattard@gmail.com<br /><br />AlbertAnonymoushttps://www.blogger.com/profile/05001967782991767837noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-50619662992522684232010-04-25T03:19:22.425+02:002010-04-25T03:19:22.425+02:00Hi Albert,
Thanks for the example, and I would ap...Hi Albert,<br /><br />Thanks for the example, and I would appreciate if you could answer my question. Is there any particular reason for using Thread.sleep(2000) in the doInBackground() method? <br /><br />The reason I am asking you this question is that I used a while loop in my doInBackground() to load vector and used publish(Vector) to work on the objects contained in the vector in the process(List) method. I used publish() in the if statement, to make sure that it gets to the publish() only if the vector is not empty. But for some strange reason, it always skipped the last valid vector and sent the empty vector to the process(). So I put some print statements in my while loop to see what's going on in there, and to my surprise it worked some of the times after that.<br /><br />To make a long story short, thinking it could be a timing issue, I removed all the debug statements and used Thread.sleep(100) to slow it<br />down a little, and viola! it worked every time after that, but I still don't know why, and I am wondering whether it will work all the time in future or not?<br /><br />MehtaUnknownhttps://www.blogger.com/profile/10383182798193028027noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-22448666156242673072009-06-02T15:00:04.905+02:002009-06-02T15:00:04.905+02:00Welcome. I love to put some more articles but I ne...Welcome. I love to put some more articles but I never find the time...<br /><br />Threads are very dependent on the environment. Both OS and load of the system may effect the outcome of the threads. Yes, like any other process, threads consume resources - these don't come for free. But when properly used, these can speed up the performance especially in parallel processing.<br /><br />An example of threads is the GC. It employs some threads. GC will kick in depending on some parameters: such as thread priority and memory availability. Being a low priority thread, it may never be invoked if the system is very busy handling higher priority threads.<br /><br />Hope this help.Anonymoushttps://www.blogger.com/profile/05001967782991767837noreply@blogger.comtag:blogger.com,1999:blog-6929422799523574458.post-12933579365276252572009-06-02T00:08:29.396+02:002009-06-02T00:08:29.396+02:00Thanks for the write up that was great. I'm curre...Thanks for the write up that was great. I'm currently using swingworker in my project. What is your opinion about threads in general in regards to time it takes to create a thread. I did some timing tests where a lot of time was spent in thread creation.Unknownhttps://www.blogger.com/profile/02991723063282902781noreply@blogger.com