Results 1 to 9 of 9
  1. #1
    Mahonroy is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Using multithreads or timers to update state of simulation?

    Hello,
    I'm working on a simple simulation which contains various nested while and for loops. I have it working to where you enter in your input, push the go button, and the simulation is completed within 1 second and displays your results.
    I need to set this up to where you can see each individual step that the program is doing along the way (e.g. one of the inputs is a step delay in milliseconds, and there are various labels that get updated so you can see whats going on).
    It seemed the way to go was to use timers, though this led to the confusion on how to code all of this inside a single timer. So then it seemed the way to go is multi-threading this somehow (trying to find various examples of each), which just led to more confusion, so I figured I would just ask you guys on how to do this? It would be the easiest (I think anyways) if I could get away with putting a sleep command in at the bottom of each loop so that the display would update (or equivalent, it seems this would be the easiest way) but I am just unsure. Any help is greatly appreciated, thanks!
    p.s. this is an applet I'm building that plays in a html page.

  2. #2
    Mahonroy is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Using multithreads or timers to update state of simulation?

    Any ideas on where to start? I'm pretty stumped

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Using multithreads or timers to update state of simulation?

    Execute the method in its own thread so that the sleep statements don't freeze up the application as a whole. When you want to update labels or whatever do that on the event dispatch thread. See SwingUtilities.invokeLater().

  4. #4
    Mahonroy is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Using multithreads or timers to update state of simulation?

    Ok I was able to make some progress. I used swing worker threads, and was able to put in sleep statements and have the screen update stuff.
    I ran into a problem though...
    I'm changing the text of labels through the jLabel.SetText("whatever text"); and everything is perfect.
    But I'm also moving labels around on the screen using the jLabel2.SetLocation(x,y);
    When I step through the program using breakpoints, everything works perfectly, but as soon as another .setText is called, the location of the panels that I had previously moved end up resetting back to their original location. So when its running, sometimes you see a label move, sometimes it just stays there, because its always being reset.
    For the life of me I can't figure out why this is happening or how to get around it, any ideas? Thanks again for the help!

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

    Default Re: Using multithreads or timers to update state of simulation?

    Do you know what layout manager is used by the container that holds the JLabels? That's likely a key issue here.

  6. #6
    Mahonroy is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Using multithreads or timers to update state of simulation?

    I was experimenting with different things, originally I tried just with "free design", then I tried it with "absolute layout", still nothing though.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Using multithreads or timers to update state of simulation?

    but as soon as another .setText is called, the location of the panels that I had previously moved end up resetting back to their original location.
    Sounds like the layout manager is being invoked and it repositions components based on the rules of the layout manager.

    I guess you need to set the layout to null.

  8. #8
    Mahonroy is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Using multithreads or timers to update state of simulation?

    You were absolutely right, I tried null layout and everything is cool now, thanks!

  9. #9
    Mahonroy is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Using multithreads or timers to update state of simulation?

    I have one last question then this project is done (and I'll post a link if you guys want to check it out).
    I put a command button in the forum labeled as "Stop" but I can't figure out how to make it stop the thread, so later on another can be created.
    I had found an example of a worker thread, and I am using it like so:

    class MeaningOfLifeFinder extends SwingWorker<String, Object> {
    @Override
    public String doInBackground() throws BadLocationException {

    I thought I could do something such as MeaningOfLifeFinder.stop(); or something but nothing seems to work. Any Ideas? Oh and here is how the worker thread is started, I just have this in a command button labeled as "start":

    (new MeaningOfLifeFinder()).execute();

    Any help is greatly appreciated, thanks!

Similar Threads

  1. I need help with timers
    By Chris Rice in forum New To Java
    Replies: 15
    Last Post: 09-04-2013, 08:02 PM
  2. Can Timers only have one task?
    By CuddlyKittens11 in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2011, 07:50 AM
  3. Replies: 2
    Last Post: 02-14-2011, 03:27 PM
  4. [SOLVED] Stop multithreads at same time
    By Niveditha in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-25-2009, 05:51 PM
  5. Stop multithreads at same time
    By Niveditha in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-25-2009, 03:42 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
  •