Reputation: 149
I am a newbie to Java and I have a gui class which has a GUI component and it takes the input from the text field and should pass it to another class. The action listener of the button is below.
public void actionPerformed(ActionEvent action) {
arraylist.add(textField_1.getText());
arraylist.add(textField_2.getText());
arraylist.add(textField_3.getText());
arraylist.add(textField_4.getText());
}
since it is a void method I cannot return the array list so that Ii cannot construct a getter.
public ArrayList<String> getList(){
return this.arraylist;
}
Could anyone please tell me how to access this arraylist from the another class without passing it through the constructor? I am sorry if i asked anything wrong. Thanks in advance.
Upvotes: 1
Views: 11750
Reputation: 31901
This is one of the many possible approaches.
Just define another class and call the setter
from your actionPerformed(..)
method.
public class YourOtherClass {
private static ArrayList<String> arraylist;
public void setList(arrayList) {
this.arraylist = arraylist;
}
public ArrayList<String> getList() {
return this.arraylist;
}
}
Now you can simply set this arraylist as:
public void actionPerformed(ActionEvent action) {
arraylist.add(textField_1.getText());
arraylist.add(textField_2.getText());
arraylist.add(textField_3.getText());
arraylist.add(textField_4.getText());
YourOtherClass.setList(arraylist);
}
Now when you want to access the contents of this list, simply use:
...
//any other method
ArrayList<String> arraylist = YourOtherClass.getList();
System.out.println(arraylist.get(0)); //or whatever
...
Upvotes: 1
Reputation: 3288
If I understood, you want to do it:
public class A {
private ArrayList<String> arrayList;
public ArrayList<String> getArrayList() {
return this.arrayList;
}
}
public class B {
private A a = new A();
public void actionPerformed(ActionEvent action) {
a.getArrayList().add(textField_1.getText());
a.getArrayList().add(textField_2.getText());
a.getArrayList().add(textField_3.getText());
a.getArrayList().add(textField_4.getText());
}
}
Upvotes: 0
Reputation: 1196
If you want to use to set and get data then there are many approaches and two of them are follow
public class SetDataInArrayList {
//Aproach one by using object
private List<ActionEvent> list;
public SetDataInArrayList() {
list = new ArrayList();
}
public void setDataInList(ActionEvent e) {
list.add(e);
}
public List<ActionEvent> getList() {
return list;
}
//Approach two by using static reference
private static List<ActionEvent> newList;
static {
newList = new ArrayList<>();
}
public static void add(ActionEvent e) {
newList.add(e);
}
public static List<ActionEvent> returnList() {
return newList;
}
if you use either of approach you will need reference variable in both of cases to fetch data
Upvotes: 0
Reputation: 23
You can make that arraylist as Static and access it.
To access that particular arraylist use the below syntax
classNameThatContainArraylist.yourArrayList
be careful while using static.
Upvotes: 0