Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So this is roughly what I want to do. But instead of add it to an ArrayList I want to add it to a class instance and then take the Array of the class instances and put it into another class


cp.add(addPetB);
      cp.add(doneOutputB);
       
      rect.setVisible(true);
       
      cbHandler = new AddPetButtonHandler();
      ebHandler = new DoneOutputButtonHandler();
 
      addPetB.addActionListener(cbHandler);
      doneOutputB.addActionListener(ebHandler);
    }
     
    //Instatiates driver method
    public static void main(String[] args)
    {
        PetInput ArE = new PetInput();
        ArE.driver();   //lines edited to meet JavaRanch restrictions on abbreviations
    }
     
     private class AddPetButtonHandler implements ActionListener
    {
       public void actionPerformed(ActionEvent e)
       {
         petName = String.parseString(nameTF.getText());
         petAge = Int.parseInt(ageTF.getText());
         petGender = //not sure how to store gender
          
         petInfo.add(petName);
         petInfo.add(petAge);
         petInfo.add(petGender);
         }


What I have tried:

I've tried putting the code into an ArrayList
Tried making a mechanism to create new class instantiations
Posted
Comments
Richard MacCutchan 21-May-22 12:19pm    
What is petInfo? Assuming you are trying to create a list of such items then it should be an instance of the Pet class.
Daniel Robinson 2022 22-May-22 4:00am    
Oh right, so I need an instance of a Pet class so that I can create different instances of this class and place the input in these instances.
Richard MacCutchan 22-May-22 4:13am    
Well that is the way most people would do it. You can then create a List<t> of those instances and process them in the same way you would a simple array. Something like:
ArrayList<Pet> pets = new ArrayList<Pet>();
Pet next = new Pet("Doberman", 12, 'M');
pets.add(next);
// etc.
Daniel Robinson 2022 22-May-22 6:54am    
Brilliant, thank you so much. That's just what I've really needed to get things going

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900