Java从数组中检索对象

时间:2014-04-17 22:59:32

标签: java arrays object jtable

我在从所述Object的数组中获取对象时遇到问题。我的代码如下。我基本上是在尝试构建一个表,并用每个对象的数据填充列。但是目前该表只显示对象而不是数据。见图:

Table image

表:

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));
        }
    });

}

对象类:

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;
}

}

2 个答案:

答案 0 :(得分:1)

要解决您的问题,您只需要一个Person的ArrayList(而不是Person数组),并且您不需要值ArrayList。

你需要的是一些方法,它创建一个表示包含数据的表的数组数组,实际上是一个字符串数组的数组。问题是&#39;列&#39;该表实际上是Person类的字段。我不确定我的解决方案(下面)是否是最好的,可能你也可以解决这个问题并处理反射。

无论如何,这是有效的(我试图尽可能地修改你的代码或简化它):

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);

    }

Person类:

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;
    }
}

我希望这会对你有所帮助。

答案 1 :(得分:0)

尝试向lineArray而不是Person个对象添加字符串数组。

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