Java - How do i modify (add/remove etc.) an ArrayList from outside of the class...
Okay so I'm working on a program here as I'm learning java,
I have an array that is initialized with 5 objects that are hard coded. I have made a GUI that takes the input needed and creates an object with those values. I need to add that object to the ArrayList that I have previously made. Okay, so I have three classes, guiclass.java, main.java and gladiator.java Objects are made and defined in "gladiator" Main contains my public static void main section, launches my gui, creates my five hard coded objects, creates my ArrayList and adds my five hard coded objects to the ArrayList. Now, I need to add the object that I generated in the guiSection [action Listener]to the ArrayList that I created in my main class's public static void main string... section. Problem is my arraylist "cannot be resolved" from guiclass. How do I do this? The ide i'm using is Eclipse. Thanks for your help. |
You could pass a reference to the ArrayList in your GuiClass constructor:
Code:
public class GuiClass{ |
I did that, but in my main class the line guiclass panel = new guiclass(); quits working, it expects something in the (), is that going to mess with my program? What is it needing?
|
Parts of guiclass:
Code:
public class guiclass extends JPanel{ Code:
public guiclass(ArrayList<gladiator> myArrayList) My Main Class Code:
import java.util.ArrayList; portion of guiclass whith listener and where i'm trying to add to the array. Code:
private class ButtonListener implements ActionListener |
In Java, if you don't explicitly define a constructor, a default, no-arg one is automatically created/assumed. Since you create a constructor
Code:
public guiclass(ArrayList<gladiator> myArrayList) Code:
guiclass panel = new guiclass(); |
edit: replied to wrong thread.... Moderators please delete.
|
I don't know if this has already been suggested ... but I would just introduce a public "add()" method to my "guiclass()":
Code:
public class guiclass extends JPanel{ "I am Spartacus" - (not) Kirk Douglas "I knew a man once who said, "Death smiles at us all. All a man can do is smile back." - Russell Crowe "Joey - do you like movies about gladiators?" - Peter Graves |
Hi, just a thought here. What is gladiator (watch the case here). If it is a class then all Classes in Java have to start with an upper-case letter. If you create a class using lower-case then Eclipse should show an error. In your code snippet gladiator and guiclass are both in lower case...
Code:
public class guiclass extends JPanel{ Code:
public class Guiclass extends JPanel{ |
tamtam -
Upper case/lower case (or, for that matter, .Net "CamelCase") are merely conventions - not rules. I would *encourage* mitchell7man to capitalize the first letter of all of his classes and, yes, "upper case vs. lower case" is a common source of errors when you're overriding class methods. *However* ... I believe the OP's class is "gladiator" IMO .. PSM |
Okay, assuming you have imported the relevant packages...
Your ArrayList object you need to initialise... Code:
private ArrayList<Gladiator> myArrayList = new ArrayList<Gladiator>() Code:
public Guiclass() |
Quote:
Oops, it does compile. When did that happen. Used to be a rule by convention. Must have missed this with the emergence of Java 5 or 6. |
So the original poster wasn't creating his ArrayList in the main method after all, plenty of solutions now though, in both upper and lowercase :)
|
Quote:
|
Thanks for the help.
I moved the ArrayList contructor into guiclass, created the gladiator objects in guiclass, added them to the array in guiclass. And am able to add to the Array from my listener.! |
All times are GMT -5. The time now is 01:39 AM. |