Results 1 to 19 of 19
  1. #1
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default SwingWorker problem. Works in Vista but not in Linux.

    I have made a countdown timer which include SwingWorker. The doInBackground and propertyChange looks like this:

    Java Code:
    public Void doInBackground() {
    	System.out.println("doInBackground = " + "Start");
        //Initialize progress property.
        setProgress(0);
        while (progress < PROGRESS_MAX) {
    
        	[B]System.out.println("doInBackground progress = " + progress);[/B]
    
            try {Thread.sleep(1000);} catch (InterruptedException ignore) {}
            countDownTimer.setOneSecondCountDown(1);
            progress = countDownTimer.getSecondsElapsed() * PROGRESS_MAX / countDownTimer.getTimerStart();
            if (progress == old){
            	if (ticTac == 1){ticTac = 0;}else{ticTac = 1;}
            }else{old = progress;}
            setProgress(Math.min(progress + ticTac, PROGRESS_MAX));
        }
        return null;
    }
    	
    public void propertyChange(PropertyChangeEvent evt) {
    
    	[B]System.out.println("propertyChange = " + evt.getPropertyName());[/B]
    
        if ("progress" == evt.getPropertyName()) { 
    
            Graphics g = getGraphics();
    	.
    	.
    	Here comes a lot of code concerning graphics, color and font
    	.
    	.
            progressBar.setValue(progress);
            progressBar.setFont(progressFont);
        } 
    }
    The problem is that when the program runs in Vista the output looks just as I would expect:

    doInBackground = Start
    doInBackground progress = 0
    propertyChange = state
    doInBackground progress = 1
    propertyChange = progress
    doInBackground progress = 3
    propertyChange = progress
    doInBackground progress = 4
    propertyChange = progress
    doInBackground progress = 6
    propertyChange = progress
    doInBackground progress = 8
    propertyChange = progress
    .
    .

    In Linux (after recompilation in Eclipse) the output is like this:

    doInBackground = Start
    doInBackground progress = 0
    doInBackground progress = 1
    propertyChange = state
    propertyChange = progress
    doInBackground progress = 3
    doInBackground progress = 4
    doInBackground progress = 6
    doInBackground progress = 8
    doInBackground progress = 9
    doInBackground progress = 11
    doInBackground progress = 12
    doInBackground progress = 14
    doInBackground progress = 16
    propertyChange = progress
    doInBackground progress = 17
    propertyChange = progress
    doInBackground progress = 19
    propertyChange = progress
    doInBackground progress = 20
    propertyChange = progress
    .
    .

    It seems as the property change is not reported to propertyChange, and it does not get better if the thread sleeps 5000 milliseconds.

    I appreciate any help.
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One problem that's evident is here:
    Java Code:
    if ("progress" == evt.getPropertyName()) {
    Don't compare Strings with == or !=. Instead use the equals or equalsIgnoreCase:
    Java Code:
    if ("progress".equals(evt.getPropertyName())) {

  3. #3
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Yes, you are right, thank you. I make that mistake now and then ... well, my first problem is still there.
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  4. #4
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    My countdown timer is build from ProgressBarDemo see How to Use Progress Bars (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components). This demo also failed on my Aspire One. It is maybe the Atom processor which isnt going well with SwingWorker. So in a minute I will close this thread.

    First I have noticed that the 'mistake' with the line 'if ("progress" == evt.getPropertyName())' also is in the demo. So I began think again. You should normally not compare stringvariables in that way because you would then compare references. But in the line are no references involved ... only two strings. So I think the line is ok. Do you agree Fubarable?
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  5. #5
    gcampton Guest

    Default

    Quote Originally Posted by pellebye View Post
    First I have noticed that the 'mistake' with the line 'if ("progress" == evt.getPropertyName())' also is in the demo. So I began think again. You should normally not compare stringvariables in that way because you would then compare references. But in the line are no references involved ... only two strings. So I think the line is ok. Do you agree Fubarable?
    You still need to use string.equals("string") to compare strings, I have also noticed that in most cases the primitive comparison operators work on strings within most windows apps, JCreator etc, but it is not good practice and should never be used. If the java tutorials are showing this it must be a typo. :cool:

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There are always two references involved whenever you use ==, just not a var that holds a reference in either case. My question is you will have to "progress" strings -- is it possible for them to be two separate distinct objects, and the answer is yes, it is possible (though not guaranteed), which sets the program up for possible pernicious intermittent errors.

    My big problem with your over all problem is in
    1) not having access to linux, so I can't test your code, and
    2) not having access to your compiled code or SSCCE, so even if I did have linex, I'd be out of luck without this.

    Hopefully you'll come up with a solution soon. Best of luck!

  7. #7
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    gcampton I agree about the good practice and Fubarable, I have not insight enough to evaluate/comment your thoughts about this matter. Anyway, I have reported the line as a typo to Sun ... so lets see what happens.

    About the original problem: I have, of course, googled a lot to find a solution. Someone out there said there was a general problem with SwingWorker running on some platforms. Unfortunately I cannot find the documentation again so I could bring it here. But I think I have to build my own sort of SwingWorker to have control over every details in the process. Sure that will be some challenge for me!
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You should be posting updates to the interface in the publish method.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you do find that article, please please bring it to us here. I would very much like to read it. Much luck!

  10. #10
    gcampton Guest

    Default

    Quote Originally Posted by Fubarable View Post
    My big problem with your over all problem is in
    1) not having access to linux, so I can't test your code!
    :O Cygwin for the win!, why be forced to choose when you can have the best of both worlds :D
    VM's are handy too.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gcampton View Post
    :O Cygwin for the win!, why be forced to choose when you can have the best of both worlds :D
    VM's are handy too.
    LOL. I'm thinking of just getting another box with Linux installed. I've been a DOS/Windows user/abuser since their inception, and I'm wondering about old dogs and new tricks.

  12. #12
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    You should be posting updates to the interface in the publish method.
    I dont have even the smallest clue about what this concern. Please give me som more information (interface and publish method).
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

  14. #14
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Thank you, r035198x, I will give that one a closer look and return.
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  15. #15
    gcampton Guest

    Default

    Quote Originally Posted by Fubarable View Post
    LOL. I'm thinking of just getting another box with Linux installed. I've been a DOS/Windows user/abuser since their inception, and I'm wondering about old dogs and new tricks.
    Well it's definitely a learning experience, to put it lightly. Windows users take so much for granted. Linux has an extreme amount of power behind it comming from a programming point of view, but at the same time it's so far away from windows, that it can be very difficult to like. A good analogy is this.

    Newbie User: I wanted a new toy car, and everybody's raving about how great Lego cars can be. So I bought some Lego, but when I got home, I just had a load of bricks and cogs and stuff in the box. Where's my car??

    Linux user: You have to build the car out of the bricks. That's the whole point of Lego.

    Newbie User: What?? I don't know how to build a car. I'm not a mechanic. How am I supposed to know how to put it all together??

    Linux user: There's a leaflet that came in the box. It tells you exactly how to put the bricks together to get a toy car. You don't need to know how, you just need to follow the instructions.

    Newbie User: Okay, I found the instructions. It's going to take me hours! Why can't they just sell it as a toy car, instead of making you have to build it??

    Linux user: Because not everybody wants to make a toy car with Lego. It can be made into anything we like. That's the whole point.

    Newbie User: I still don't see why they can't supply it as a car so people who want a car have got one, and other people can take it apart if they want to. Anyway, I finally got it put together, but some bits come off occasionally. What do I do about this? Can I glue it?

    Linux user: It's Lego. It's designed to come apart. That's the whole point.

    Newbie : But I don't want it to come apart. I just want a toy car!

    Linux user: Then why on Earth did you buy a box of Lego??

    I lol'd at this hard when I first read it, but I had been using Linux for a year or two as well, so it made a lot of sense to me. We are all newbies at some point in time.

    Cygwin is pretty cool, because it's just an emulation layer you can intergrate it directly into windows, for all your typical utilities, But you as far as desktop GUI's are concerned it's not too functional. Possible, but hard to set up due to major configuration of X-server. Cygwin full install is about 3.5gb much like any standard distrobution. If you are planning on making a linux box, then I suggest Linux Mint as it is nice and comes with most libraries and compiling tools already, More than any other distro I have used (i have tested about 30 main ones)

    Also another option is OpenSolaris which isn't quite Linux, but it's close enough, I have not been able to get this to work, I stuck the release candidate in my laptop and my CPU alarm went off and broke my speakers, which I wasn't too pleased about, but then I don't listen to much music anyway. So I tried a dev build USB install, which could only see my USB, it couldn't see my hardrive so I couldn't install anywhere ... 3rd time Lucky, the installer kept crashing and when it finally did work I kept getting kernel panic - unable to sync errors.... So gave up on Sun's operating system until they start getting a bit more stable. Had similar problems with FreeBSD as well, which is a lot like solaris.

    IMO I think these are the 2 best stable platforms:-
    Linux Mint, for new computers produced in the last 5 years or so, and Puppy Linux if you are running on ancient hardware, 32mb ram, 100mb hdd etc... Puppy linux is a 30mb operating system with an incredibly small footprint, but heaps of functionality, probably more than a standard vista install.

    EDIT: sorry to the OP for trolling it's unintentional :D

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for the info!

  17. #17
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Basically there seems to be two ways to communicate between a worker thread and the EDT when using SwingWorker.

    The property way:
    One way is to use the progress property. You have to add a propertyChangeListener and with setProgress(value) the progress property is initialized. When the progress property change to another value an event will be sent to propertyChange(event). That is the way I have used in my countdown timer. The ProgressBarDemo on How to Use Progress Bars (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) also uses this way.

    The publish way:
    As r035198x pointed out there is another way to communicate between the worker thread and the EDT. Here the values you want to deal with on the EDT are published by using SwingWorkers publish method which send a list to the process method. Usefull when you have more than one result to send to the EDT. The Flipper demo on Tasks that Have Interim Results (The Java™ Tutorials > Creating a GUI With JFC/Swing > Concurrency in Swing) uses this way (no progress bar). And a SwingWorkerExample SwingWorker example code also uses this way (with progress bar). The last one is terrible formattet but can be downloaded formattet on http://pellebye.dk/javaForum/SwingWorkerExample.java.

    All of the demos and my countdown timer run intermittent on my Aspire One with Linux Linpus. Sometimes they run just as on my Vista pc other times they get started after a few seconds having skipped some results, but mostly they dont run at all.

    Anyway, thank you all for your help.
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by pellebye View Post
    Basically there seems to be two ways to communicate between a worker thread and the EDT when using SwingWorker.

    The property way:
    One way is to use the progress property. You have to add a propertyChangeListener and with setProgress(value) the progress property is initialized. When the progress property change to another value an event will be sent to propertyChange(event). That is the way I have used in my countdown timer. The ProgressBarDemo on How to Use Progress Bars (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) also uses this way.
    ....
    But you don't update that way for intermediate results
    Read the API specs part which says
    Quote Originally Posted by API Specs
    Because PropertyChangeListeners are notified asynchronously on the Event Dispatch Thread multiple invocations to the setProgress method might occur before any PropertyChangeListeners are invoked. For performance purposes all these invocations are coalesced into one invocation with the last invocation argument only.

    For example, the following invokations:

    setProgress(1);
    setProgress(2);
    setProgress(3);

    might result in a single PropertyChangeListener notification with the value 3.

  19. #19
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    So, do you think that in the publish way the process method will process every intermediate result? In that case the working thread has to/may wait for the update before calculating the next intermediate result ... or there will/can be a queue to the process method.

    Maybe you are right. So thats a difference between the two ways, too. In connection with my countdown timer it is importent that the count of seconds is correct. So the working thread must be steady ongoing. And that is also why I should choose the property way.

    Yes, I think you are right. Good point. Until now, I could not see any big differences between the two ways. Thank you very much. The first thing to learn is not to read the text, but to READ the text.
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

Similar Threads

  1. SwingWorker Problem
    By Berkan in forum Threads and Synchronization
    Replies: 10
    Last Post: 03-11-2010, 03:28 AM
  2. java.util.Scanner runs in Vista but not in Linux
    By pellebye in forum New To Java
    Replies: 6
    Last Post: 08-24-2009, 03:50 AM
  3. [SOLVED] Problem with socket programming(Vista)
    By jagatsastry in forum Networking
    Replies: 1
    Last Post: 02-02-2009, 03:56 PM
  4. Replies: 3
    Last Post: 10-02-2008, 05:48 AM
  5. problem with ServerSocket on Linux
    By gabriel in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •