Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store input data from GUI into arraylist?

 
Brian Mart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm still working on getting the basics of Java down and I do not understand arrays completely. I am working an a GUI where a user can input data for as many people as the user likes (int age, boolean gender[from a combobox], and String name) and store all of that data into an ArrayList. The other button of the GUI outputs the ArrayList to a text file, but I know how to do that. What examples or API classes can I look at to understand this and write my AddButtonHandler code?
Edit for clarity and some additional code

 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you planning to save the data to an ArrayList? Saving the data in a class named Pet - or perhaps an extension of Pet like Pet.Dog, Pet.Cat, Pet.GuineaPig, etc., if there's a reason to subclass Pet - would make more sense and would be more in keeping with an OOP design.

Edit: You could then store the resulting classes, whether subclassed or not, into an ArrayList of Pet, ArrayList<Pet>.
 
Brian Mart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, your answer has left me a little confused. I already have a class called AddPetButtonHandler that has an ActionListener, and I am looking to get this class to store the information that is typed ( using .getText() ) but I do not understand how to transfer this gotten text into an array.
Also, this is the first time I have used a combobox and I am unsure of how to store that infomation, as I cannot use .getText() to store the boolean value of the combobox
For a better idea of what I am trying to do, here is a slightly updated code:
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your handler class ought to be named XXXXListener, because it is a Listener class. Just as subclasses of the Exception class ought to be named XXXException.
String.parseString(xxx.getText)) ???? What does that do? Take a String and turn it into a String? I didn’t even know there was such a method, but turning a String into a String should be much simpler than that.
You have completely misunderstood the previous post. You should be looking at. . . or similar
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:. . . I didn’t even know there was such a method . . .
I still can’t find it in the API; I don’t think it exists.
 
Brian Mart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote that code to ensure that everything in that field was a String, no matter what. Anyway, I have had time to complete this code and fix my problems over the past few days. Thanks for your help
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use getText() and it says it returns a String, then you get a String. You need to learn your way round the API documentation and to get used to reading it. I still don’t think you will find a parseString method anywhere.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic