0

I'm having an issue fetching an object from an array of said Object. My code is below. I'm basically trying to build a table and fill in the columns with the data from each object. However currently the table is just showing the object and not the data. See picture:

Table image

Table:

public void buildTable(JPanel panel) {

    File file = new File("C:\\Users\\user\\Desktop\\test.txt");
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        lineArray = new ArrayList<Person[]>();
        while((line = br.readLine()) != null) {
            String[] temp = line.split("\\t");
            Person p = new Person(temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8]);
            lineArray.add(new Person[]{p});
        }
        br.close();
    } catch (FileNotFoundException e) { e.printStackTrace();} 
    catch (IOException e) { e.printStackTrace(); }

    List<String> columns = new ArrayList<String>();
    final List<Person[]> values = new ArrayList<Person[]>();

    columns.add("First Name");
    columns.add("Last Name");
    columns.add("Address");
    columns.add("Address 2");
    columns.add("City");
    columns.add("State");
    columns.add("Zip Code");
    columns.add("Phone");
    columns.add("Email");

    for (int i = 0; i < lineArray.size(); i++) {
        values.add(lineArray.get(i)); //this is the line where I'm guessing the main problem is
    }

    TableModel tableModel = new DefaultTableModel(values.toArray(new Object[][] {}), columns.toArray());
    final JTable table = new JTable(tableModel);

    JScrollPane tableContainer = new JScrollPane(table);
    tableContainer.setBounds(10, 36, 833, 219);
    panel.add(tableContainer);

    table.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            int selectedRow = table.convertRowIndexToModel(table.getSelectedRow());
            contactName.setText((String) table.getValueAt(selectedRow, 0) + " " + table.getValueAt(selectedRow, 1));
            contactAddress.setText((String) table.getValueAt(selectedRow, 2));
        }
    });

}

Object Class:

public class Person {

public String firstName;
public String lastName;
public String address;
public String address2;
public String city;
public String state;
public String zip;
public String phone;
public String email;

public Person(String firstName, String lastName, String address, String address2, String city, String state, String zip, String phone, String email) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.address2 = address2;
    this.city = city;
    this.state = state;
    this.zip = zip;
    this.phone = phone;
    this.email = email;
}

}

3
  • Looking at your code looks like you have design issue. lineArray is arraylist of Person array. lineArray = new ArrayList<Person[]>(); I think you just have to add Person object to your array list. Commented Apr 17, 2014 at 23:17
  • I am adding a Person object to the arraylist lineArray.add(new Person[]{p}); Commented Apr 17, 2014 at 23:28
  • Long story short.. lineArray.add(p); and define your arraylist as lineArray = new ArrayList<Person>(); Commented Apr 17, 2014 at 23:31

2 Answers 2

1

To solve your problem, you need only ONE ArrayList of Person (and not of array of Person), and you don't need the value ArrayList.

What you need instead is some method that create an array of arrays representing a table containing your data, in fact an array of arrays of Strings. The problem is the 'columns' of the table are actually the fields of the Person class. I'm not sure if my solution (below) is the best, probably you can solve this problem also dealing with the reflection.

Anyway, this works (I tried to modify least as possible your code or to simplify it):

public void buildTable(JPanel panel) throws IOException {
        File file = new File("C:\\Users\\user\\Desktop\\test.txt");
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            lineArray = new ArrayList<Person>();
            while ((line = br.readLine()) != null) {
                String[] temp = line.split(",");
                Person p = new Person(temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8]);
                lineArray.add(p);
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<String> columns = new ArrayList<String>();


        columns.add("First Name");
        columns.add("Last Name");
        columns.add("Address");
        columns.add("Address 2");
        columns.add("City");
        columns.add("State");
        columns.add("Zip Code");
        columns.add("Phone");
        columns.add("Email");

        TableModel tableModel = new DefaultTableModel(Person.toStringsMatrix(lineArray), columns.toArray());
        final JTable table = new JTable(tableModel);

        JScrollPane tableContainer = new JScrollPane(table);
        tableContainer.setBounds(10, 36, 833, 219);
        panel.add(tableContainer);

    }

And the Person class:

class Person {

    public String firstName;
    public String lastName;
    public String address;
    public String address2;
    public String city;
    public String state;
    public String zip;
    public String phone;
    public String email;

    public Person(String firstName, String lastName, String address, String address2, String city, String state, String zip, String phone, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.address2 = address2;
        this.city = city;
        this.state = state;
        this.zip = zip;
        this.phone = phone;
        this.email = email;

    }

    public String[] toStrings() {
        return new String[]{firstName, lastName, address, address2, city, state, zip, phone, email};
    }

    public static String[][] toStringsMatrix(ArrayList<Person> personList) {
        int rows = personList.size();
        String[] first = personList.get(0).toStrings();
        int columns = first.length;
        String[][] table = new String[rows][columns];
        table[0] = first;
        for (int k = 1; k < rows; k++) {
            table[k] = personList.get(k).toStrings();
        }
        return table;
    }
}

I hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

0

Try adding a array of strings to your lineArray instead of Person objects.

lineArray.add(new String[]{temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8]});

1 Comment

I want to use the Object for easier access later though since I have to pass around info and pull information in different areas

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.