简体   繁体   中英

Java GUI aplication, load data to Jtable from a list<objects>

Java GUI aplication, load data to Jtable from a list i have found the following link but i haven't found an answer: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

please can someone gice me an example how should i do it. my objects have 5 fields:Name,Grade,Salary,BirthYear,Sex and the list is readed from a file so i dont know how many ojects will the List have. I am working in netbeans.

The tutorial you linked to has an example: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data . You should be able to adapt it to a List<Employee> easily:

public int getRowCount() { 
    return list.size();
}

public int getColumnCount() {
    return 5; 
}

public Object getValueAt(int row, int col) {
    Employee employee = list.get(row);
    if (col == 0) {
        return e.getName();
    }
    else if (col == 1) {
        return e.getGrade();
    }
    ...
}

Using vector should help you. Load an array to a Java table .

Vector model = new Vector();
Vector row = new Vector();

row.add("abce");
row.add("def");
row.add("ghi");
model.add(row);

row = new Vector();
row.add("sds");
row.add("sdfds");
row.add("24");
model.add(row);

JTable table = new JTable(model);

All the methods of Vector is synchronized. But, the methods of ArrayList is not synchronized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM