1

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.

4
  • Does the arraylist in the actionPerformed() belong to any other class? if the arraylist and the actionPerformed() belong to the same class, I think the getList() method will work. Commented Oct 12, 2016 at 3:27
  • No the arraylist in actionPerformed() belongs to the gui class only.But the problem is it cannot be modified because the void method does not return the modified arraylist.Because call by value in java Commented Oct 12, 2016 at 3:30
  • what "other class" that is? Normally we won't see it as "passing to another class", we see things like "passing to another OBJECT INSTANCE" instead. What that other object is? How are you referring to it? Or are you creating that object instance in the action listener? Be clear on your question Commented Oct 12, 2016 at 3:34
  • Why does a void method prevent a getter method? The Arraylist is out of the scope of the void method. You don't need to return it, and you can write a getter Commented Oct 12, 2016 at 4:04

4 Answers 4

1

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
...
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

-1, static is a memory tool (not a convenience one), and you shouldn't promote the idea of exposing fields (especially mutable ones) like that.
0

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

Comments

0

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());     
    }
}

2 Comments

it will be better design pattern if you'll use setter method too like void setList(ActionEvent action){list.add(..)}
Sure, @AsteriskNinja. It would be much better. But I was just trying to answer the main question: "How to access arraylist from another class"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.