Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Using Threads

using thread

  • Please log in to reply
16 replies to this topic

#13 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 24 November 2011 - 11:26 PM

What does the actionPerformed(ActionEvent e) method look like?
  • 0

#14 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 25 November 2011 - 06:37 AM

Ok, excuses me for incomplete information. So let me explain the whole idea. I have one class that has an ArrayList and keep a record (history) of every pattern that user made by making moves. I have a GUI class that extends JFrame and the only work there is receiving a pattern each time and update its view and a third class that extends ActionPerformed and when a button clicked doing a different job. For example, if click a square on the board, modifying the current state in order to looks like the user's choice, and store this new pattern in the ArrayList. Now I'm stuck on a point that a button (Replay) when pressed, should start from the very first pattern in the ArrayList and every 1 sec I want to move to the next(update the GUI) until reach the latest pattern. Here for a reason my update not working properly on the GUI (but if print out the patterns on "console" working perfect) stuck at the beginning, and not show some patterns. My question is how to fix this, where to create new Thread ? In the ActionPerformed class where Replay button executed, or in JFrame class where GUI ceated?
Please help
Thank you
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#15 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 25 November 2011 - 06:59 AM

toto_7 you think you could post the classes. There could be other issues. It's a bit difficult sometimes, to see the big picture from all these code snippets and explanations.
  • 0

#16 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 25 November 2011 - 08:36 AM

Well, the fact that your GUI doesn't update means you're doing stuff in the actionperformed which you shouldn't be doing there. So again, Post the actionPerformed code please.
  • 0

#17 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 27 November 2011 - 03:15 AM

Sorry guys, away from my machine for a while. Ok, here is my code in ActionPerformed for "Replay" button:

public void actionPerformed(ActionEvent e) {
     if(e.getActionCommand().equals("replay")){
          if(view.replay.getIcon().toString().equals(replay.toString())){//If Icon is REPLAY
               while(!view.listOfUndos.isEmpty()){//if user made some undo
                    moves++;
                    int size = view.listOfUndos.size();
                    String lastUndo = view.listOfUndos.get(size-1);
                    view.listOfMoves.add(lastUndo);//Bring them back to list of moves
                    view.listOfUndos.remove(size-1);//remove them
               }
               view.replay.setIcon(stop);//Change the Icon to STOP(Also now my program not changing Icon)
               SwingUtilities.invokeLater(Replay);//Do the Replay
          }
          else
               view.replay.setIcon(stop);
     }
}

Runnable Replay = new Runnable(){
     public void run() {
          for(int i=0; i<model.userStates.size(); i++){//For the size of list of Patterns(History)
               synchronized(model){//Notify the other class(Contains method to notify the GUI and update it with new pattern)
                    model.currentState = model.userStates.get(i);//Make the current equal to i
                    model.notify();//Notify the model class 
               }
               try {
                    Thread.sleep(1000);
               } catch (InterruptedException e) {}
          }
     }
};

As you can see, modified my previous code a little. I don't know if now is worst or not but the result is, NO REPLAY. Checked and I'm sure that each pattern from the list is going to GUI for initialization but for a reason, GUI has always the "shape" of the very last pattern. How to change my code in order to work?

Thank you
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz





Also tagged with one or more of these keywords: using thread

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download