DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Thread: For Loop Help

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    12

    For Loop Help

    i'm using a for loop to iterate through an Arraylist of objects...
    each object is a new window, and only want the for loop to continue after I close each new object..
    i can't figure out how to do that, anyone got an idea?

    here's where the for loop is.
    Code:
     public void actionPerformed (ActionEvent guiEvent) 
       {
          if (guiEvent.getActionCommand ().equals("Add")) 
          {  // determine origin of event
    	      if (choice1Prompt.getText().equals(answerPrompt.getText()))
    	      {
    	           Card localCard = new Card();
    	           localCard.setQuestion(questionPrompt.getText());
    	           localCard.setChoice1(choice1Prompt.getText());
    	           localCard.setChoice2(choice2Prompt.getText());
    	           localCard.setChoice3(choice3Prompt.getText());
    	           localCard.setChoice4(choice4Prompt.getText());
    	           localCard.setAnswer(answerPrompt.getText());
    	           cardList.add(localCard);  // create cardList arrayList element object
    	      }
    	      else if (choice2Prompt.getText().equals(answerPrompt.getText()))
    	      {
    	      	   Card localCard = new Card();
    	           localCard.setQuestion(questionPrompt.getText());
    	           localCard.setChoice1(choice1Prompt.getText());
    	           localCard.setChoice2(choice2Prompt.getText());
    	           localCard.setChoice3(choice3Prompt.getText());
    	           localCard.setChoice4(choice4Prompt.getText());
    	           localCard.setAnswer(answerPrompt.getText());      	
    	           cardList.add(localCard);  // create cardList arrayList element object
    	      }
    	      else if (choice3Prompt.getText().equals(answerPrompt.getText()))
    	      {
    	      	   Card localCard = new Card();
    	           localCard.setQuestion(questionPrompt.getText());
    	           localCard.setChoice1(choice1Prompt.getText());
    	           localCard.setChoice2(choice2Prompt.getText());
    	           localCard.setChoice3(choice3Prompt.getText());
    	           localCard.setChoice4(choice4Prompt.getText());
    	           localCard.setAnswer(answerPrompt.getText());      	
    	           cardList.add(localCard);  // create cardList arrayList element object
    
    
    	      }
    	      else if(choice4Prompt.getText().equals(answerPrompt.getText()))
    	      {
    	      	   Card localCard = new Card();
    	           localCard.setQuestion(questionPrompt.getText());
    	           localCard.setChoice1(choice1Prompt.getText());
    	           localCard.setChoice2(choice2Prompt.getText());
    	           localCard.setChoice3(choice3Prompt.getText());
    	           localCard.setChoice4(choice4Prompt.getText());
    	           localCard.setAnswer(answerPrompt.getText());      	
    	           cardList.add(localCard);  // create cardList arrayList element object
    	      }
    	      else
    	      {
    	      		JOptionPane.showMessageDialog(null, "Answer does not equal one of the choices.\nYou must type in an answer equal to one of the choices.");
    	      }
    
                  // reinitialize input fields
                  answerPrompt.setText("... enter answer...");
                  answerPrompt.selectAll();
                  answerPrompt.requestFocus();
          }
    
          else if (guiEvent.getActionCommand().equals("Use FlashCards")) 
          {  // determine origin of event
          		
          		boolean finished = false;
          		
    			if (!(cardList.isEmpty()))
    			{
    				for (Iterator<Card> count = cardList.iterator(); count.hasNext();)
            		{
            			Card localCard = (Card)count.next();
            			CardPanel localPanel = new CardPanel(localCard);
            		}
    			}
    			else
    			{
    				JOptionPane.showMessageDialog (null, "No FlashCards Loaded!");
    			}
          }
    
          else if (guiEvent.getActionCommand().equals("Exit")) 
          {
             System.out.println("\nThank you and good bye!\n");
             System.exit(0); // terminate program normally
          }
        }
    here's all the code, in case some needs it:
    http://members.lycos.co.uk/naazrael/FlashCardsPro.zip
    Last edited by naazrael; 12-14-2005 at 10:34 AM.

  2. #2
    Join Date
    Mar 2005
    Posts
    24
    I didn't really look at all the code.. but from reading the question:

    could you not just say something to the effect of:
    while(theArrayList.get(i).isVisible() == true){}

    which would loop until the window was closed. Or you could do it more eloquently with WindowClosing events.. but it seems like the first option would work

  3. #3
    Join Date
    Oct 2005
    Posts
    12
    well, i'll check out that while loop...
    i can't compile anything right now, so yeah.

    but, just to clarify, i want this code:

    Code:
    		for (Iterator<Card> count = cardList.iterator(); count.hasNext();)
            		{
            			Card localCard = (Card)count.next();
            			CardPanel localPanel = new CardPanel(localCard);
            		}
    the localCard just stores the data, and the localPanel is the GUI for localCard.
    i want this code to create a localPanel object, and only create a new object after that first localPanel is closed.
    Last edited by naazrael; 12-14-2005 at 10:26 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links