QUOTE(Rassti @ 1 Sep, 2008 - 10:01 PM)

i have << String s = "Name: " + getName() + "Surname " + getSurname() + "\nId: " + getId() + "\nAge: " + getAge(); >> of Person class.
I displayed it using JTextArea but i need to display it in table (using JTable)
i have class to add new person,it's done by using ArrayList
......
public class AddPerson
{
private ArrayList<Person> pnr = null;
public Person()
{
psnr = new ArrayList<Prisoner>();
}
public void addPerson(Person person)
{
pnr.add(person);
}
.....
and i tried display all persons in table,:
public class DisplayAllUsers {
public static void main(String args[]) {
JFrame f = new JFrame("List of users");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
Object rows[][] = {person.name, person.surname, person.id, person.age }; // i have troubles with this line)
Object columns[] = { "Name", "Surname", "ID","Age" };
JTable table = new JTable(rows, columns);
JScrollPane scrollPane = new JScrollPane(table);
content.add(scrollPane, BorderLayout.CENTER);
f.setSize(700, 200);
f.setVisible(true);
}
}
has someone any idea about it ??
Some things are wrong/weird in your code
CODE
// this defines the class AddPerson
public class AddPerson
{
private ArrayList<Person> pnr = null;
// this looks like a constructor for a Person object it has nothing to do in the definition of class AddPerson
public Person()
{
psnr = new ArrayList<Prisoner>();
}
// ok a void addPerson in class AddPerson (be carefull with the case of your letters it can be dangerous)
public void addPerson(Person person)
{
pnr.add(person);
}
I thing what you try to do is:
CODE
public class Person {
static ArrayList<Person> array = new ArrayList<Person>();
String name;
int id;
Person(String name, int id) {
this.name = name;
this.id = id;
array.add(this);
}
}
As far as your
CODE
<< String s = "Name: " + getName() + "Surname " + getSurname() + "\nId: " + getId() + "\nAge: " + getAge(); >> of Person class.
This is probably the toString() method of your class person. You cannot use it to populate your JTable
you will have to loop thru you psnr and extract each element
CODE
TableModel model = jTable.getTableModel();
for(int i = 0; i < psnr.size(); i++) {
// extract each element to populate the JTable
Person p = psnr.get(i);
model.setValueAt(p.getName(), i, 0); // col 0
model.setValueAt(p.getId(), i, 1); // col 1
}