Hello, I am trying to override the toString() method and then output an ArrayList to a JTextArea swing component. I will output the list to a gui using an action, but for the method itself, what should the return statement be? I am using return type String on the method, but I don't want to output to the command window. I understand part of the problem lies within incompatible types, but can I have a return of type ArrayList? Is there a generic return to use to satiate system so that I may append to JTextArea in the GUI?>

I am using return type String on the method, but I don't want to output to the command window.

Return a String, you can do whatever you want with it when you get it: print it to the console, stick it in a TextArea, parse it for XML, hash the characters into a binary bitstream and convert it into a Life initial configuration and see if it does anything interesting, whatever you like. It won't go to the console unless you send it there with a println.

I understand part of the problem lies within incompatible types, but can I have a return of type ArrayList

You can have a method called toString() that returns an ArrayList, but that seems somewhat perverse, and it would NOT override the toString method, because the signature would be different. I'd call it "toStrings", myself, or maybe "toArrayList". If you do this, the return will be an ArrayList<String>, and you can do what you like with that, including appending each of the Strings into your TextArea. There may be reasons to do this - you might want to control which Strings go in and which don't, for example. But it's probably easier to just append a String, which you'll get from your toString().

Just to add something to jon.kiparsky's good advice, each object has a toString method, whether it is overriden or not. Even the ArrayList.
If an ArrayList has String objects:

ArrayList<String> list = new ArrayList<String>();

list.add("aaaa");
list.add("bbbb");

// They both do the same
System.out.println(list);
//or
System.out.println(list.toString());

Then the above code will print all the Strings in the list, because that is how the toString method of the class ArrayList is overridden. What it does, it returns all the Strings in that list.
Meaning,
if you add any other object in the ArrayList, then again, the toString method of the ArrayList, will call the toString method of each of the objects in the list and return that:

Try this.

class Person {
  private String name = null;
  private int age = null;

  public Person(String n, int a) {
     name = n;
     age = a;
  }

  public String toString() {
     return name+" is "+age+" years old";
  }
}
ArrayList<Person> list = new ArrayList<Person>();

list.add(new Person("Aaaaa", 10));
list.add(new Person("Bbbbb", 20));

System.out.println(list);

So you can call the toString method of the list and append that to the JTextArea, or you can loop it and append each object in the list in any way you want.

In addition you can look the class JList. You can also add objects in the way you add them to an ArrayList and the toString method of each of those objects will be displayed like a list. It has a better looking style than the JTextArea, where you must handle the line change and formatting on your own

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.