aspose file tools*
The moose likes Beginning Java and the fly likes How to store input data from GUI into arraylist? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to store input data from GUI into arraylist?" Watch "How to store input data from GUI into arraylist?" New topic
Author

How to store input data from GUI into arraylist?

Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
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

Joined: Oct 24, 2010
Posts: 563
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>.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
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

Joined: Oct 13, 2005
Posts: 40034
    
  28
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

Joined: Oct 13, 2005
Posts: 40034
    
  28
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

Joined: Apr 17, 2012
Posts: 34
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

Joined: Oct 13, 2005
Posts: 40034
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store input data from GUI into arraylist?