i am very new in java.
in GUI class i have code like this :

public void actionPerformed(ActionEvent e) {
if(e.getSource()==listButton){

ArrayList<String> list = new ArrayList<String>(); 
list.add("Orange"); 
list.add("Apple"); 
list.add("Pineapple"); 
list.add("Lemon"); 
list.add("Pear"); 
for(int i=0; i < list.size(); i++) { 
String s = list.get(i);
   { textArea.setText(s+"\n"); }
}

but its showing only the last name 'Pear' in my textArea but it suppose to show the list of names.
why i am getting this output ???
anybody can tell me where i am doing wrong..............

i also tried like this :

public void actionPerformed(ActionEvent e) {
if(e.getSource()==listButton){

ArrayList<String> list = new ArrayList<String>(); 
list.add("Orange"); 
list.add("Apple"); 
list.add("Pineapple"); 
list.add("Lemon"); 
list.add("Pear"); 
for(String s : list) { // read as for each s in list 
                         textArea.setText (s);
            }

The setText method does exactly what it says; it SETs the text of the TextArea. So The TextArea will have whatever value you put as argument.

You can either, create a String with all the values and then set that String
OR BETTER:
Use the append method of the TextArea.

Check the API of JTextArea for that method and replace the textArea method with that.

Also next time when you are using a method that doesn't do what you want, check the API of the class

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.