Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,376 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,495 people online right now. Registration is fast and FREE... Join Now!




display string object in table using JTble

 
Reply to this topicStart new topic

display string object in table using JTble

Rating  5
Rassti
1 Sep, 2008 - 09:01 PM
Post #1

New D.I.C Head
*

Joined: 1 Sep, 2008
Posts: 2

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 ??



User is offlineProfile CardPM
+Quote Post

pbl
RE: Display String Object In Table Using JTble
2 Sep, 2008 - 04:55 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
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
}

User is offlineProfile CardPM
+Quote Post

Unknown Hero
RE: Display String Object In Table Using JTble
4 Sep, 2008 - 03:33 PM
Post #3

New D.I.C Head
Group Icon

Joined: 4 Sep, 2007
Posts: 37



Thanked: 7 times
Dream Kudos: 50
My Contributions
Do it on the way I already told you:

http://www.dreamincode.net/forums/index.ph...st&p=410771

Create your own model and then manipulate with the table through the model.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:02AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month