PDA

Click to See Complete Forum and Search --> : How to display ArrayList on new line


tommie20
Feb 11th, 2010, 10:29 AM
Hi Guys,

I have the following bit of code

package Final;


import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;


// Hieronder staan de verschillende tekstvakken, buttons en arraylists die ik gebruik.

class Paneel2 extends JPanel
{
private JTextField tekstvak1;
private JTextField tekstvak2;
private JTextArea uitvoerA;
private JTextArea uitvoerB;
private JButton toernooiButton;
private JButton weergaveButton;
private Badminton[] ArrayShuttle;
private int count = 0;






public Paneel2 ()
{
// Hieronder staan de textarea's, buttons en texfields gedefineerd

JLabel toernooiplek = new JLabel("Toernooi plek: ");
tekstvak1 = new JTextField(20);
JPanel plekPanel = new JPanel();
plekPanel.add(toernooiplek);
plekPanel.add(tekstvak1);

JLabel toernooinaam = new JLabel ("Toernooi naam: ");
tekstvak2 = new JTextField(20);
JPanel tekstPanel = new JPanel();
tekstPanel.add(toernooinaam);
tekstPanel.add(tekstvak2);

uitvoerA = new JTextArea(10,10);
tekstPanel.add(uitvoerA);
uitvoerA.setOpaque(false);
uitvoerA.setEditable(false);

toernooiButton = new JButton ("Voeg toernooi toe");
toernooiButton.addActionListener( new KnopHandler() );

weergaveButton = new JButton (" Geef Toernooien weer");
weergaveButton.addActionListener(new KnopHandler2());


uitvoerB = new JTextArea(10,10);
plekPanel.add(uitvoerB);
uitvoerB.setOpaque(false);
uitvoerB.setEditable(false);



ArrayShuttle = new Badminton[20];

// manier van actionlisteners toevoegen zonder ze nader te definieren


add(tekstPanel);
add(plekPanel);
add(weergaveButton);
add(toernooiButton);



// voor alle knoppen heb ik if - else gebruikt.

}

class KnopHandler implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
String setPlek = tekstvak1.getText();
String setToernooi = tekstvak2.getText();
ArrayShuttle[count] = new Badminton(setPlek, setToernooi);
count++;
tekstvak1.setText("");
tekstvak2.setText("");


JOptionPane.showMessageDialog(null, "Toernooi Toegevoegd");
}

}

class KnopHandler2 implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
for (int i = 0; i<ArrayShuttle.length;i++)
{

uitvoerA.setText(ArrayShuttle[i].getPlek()+ "\n");
uitvoerB.setText(ArrayShuttle[i].getToernooi() + "\n");
}
}
}
}







This is the piece of code is important


class KnopHandler2 implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
for (int i = 0; i<ArrayShuttle.length;i++)
{

uitvoerA.setText(ArrayShuttle[i].getPlek()+ "\n");
uitvoerB.setText(ArrayShuttle[i].getToernooi() + "\n");
}
}
}

If i run the program, it overwrites the same line everytime.
How do i display the line below the other etc etc.

Thanks guys,

Tom

kfcSmitty
Feb 11th, 2010, 04:19 PM
You want to use append instead of setText on your JTextArea.

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTextArea.html

setText will replace what is in there with the whatever you're setting it to.

tommie20
Feb 11th, 2010, 05:34 PM
thanks kfcSmitty,

The only problem now is, if i show to array it will give me all the entries.
But if i add another entry and press show again it will just put them below the previous ones. So you have duplicates.
Are there any simple solutions for this issue ( i'm looking at it my self atm )

Tom

ComputerJy
Feb 12th, 2010, 04:55 AM
You can add a flag for when entries change then setText to ""

tommie20
Feb 12th, 2010, 05:11 AM
i figured it out , and it worked.
I indeed used the setText("")

Thanks for the help again

Tom