user3364528
user3364528

Reputation: 27

Array to String to append to a textArea in a JFrame GUI?

I have an Arraylist<ActivityPerformed> activityPerformed that I want to print into a textArea as a part of a String printReport().

This is what I have right now (located in Tracker.class)

public String getActivityPerformed() {
    String done = "";
    String error = "No tracked activities.\n";
    for(int i = 0; i < activityPerformed.size(); i++) {
        done += activityPerformed.get(i);
        done += "\n";
        return done;
    }
    return error;
}

public String printReport()
{   
    lifestyleReport  = "\n \nLifestyle Report \n";
    lifestyleReport += "Activities Performed \n" + getActivityPerformed();
    return lifestyleReport;
}

}

And this is what I have for the GUI (TrackerGUI.class):

public class Report implements ActionListener {
    public void actionPerformed (ActionEvent ae) {
        if(tracker.foodEaten.isEmpty() == true && tracker.activityPerformed.isEmpty() == true)
            textArea.append("Error: No food/activity tracked. \n");
        else textArea.append(tracker.printReport());
    }
}

What do I add to the code to make the array print into the textArea properly? I always get something like this: @365bc793

Upvotes: 0

Views: 1320

Answers (2)

JB Nizet
JB Nizet

Reputation: 692071

Each time an object is transformed to a String by concatenation, as in

done += activityPerformed.get(i);

its toString() method is called. Every object has such a method, because all Java classes inherit from java.lang.Object, which has a default implementation of this method. This default implementation, however, only returns the type of the object and its hashCode. So, if you want to return something different, you have to override toString() in your class:

@Override
public String toString() {
    return "Hello, I'm an ActivityPerformed, and my name is " + this.name;
}

So, either do that, or print specific values from your object:

for(int i = 0; i < activityPerformed.size(); i++) {
    ActivityPerformed a = activityPerformed.get(i)
    done += a.getName();
    done += " - "
    done += a.getFoo();
    done += "\n";
}

Also, note that you should favor the foreach loop:

for (ActivityPerformed a : activityPerformed) {
    done += ...
}

Also note that you shouldn't return from inside the loop, otherwise you'll only have the first element of the list in your string.

And finally, you should use a StringBuilder to make your code efficient. Not doing that will create a whole lot of temporary objects and be inefficient:

StringBuilder builder = new StringBuilder();
for (ActivityPerformed a : activityPerformed) {
    builder.append(...);
}
return builder.toString();

Upvotes: 1

Kick
Kick

Reputation: 4923

  for(int i = 0; i < activityPerformed.size(); i++) {
            done += activityPerformed.get(i);
                                        ^------------Object of ActivityPerformed
            done += "\n";
            return done;
        }

In the above code, you are getting the element index i from the list but the type is of ActivityPerformed due to which when you print on textarea the object get printed.

Upvotes: 1

Related Questions