1

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.

LogofaT
  • 289
  • 1
  • 3
  • 17

2 Answers2

1

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();
    }
    ...
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • This related [example](http://stackoverflow.com/a/9134371/230513) illustrates using a `Map`. – trashgod Dec 02 '12 at 15:55
  • im a bit new at java,so i whould have another question netbeans automaticaly generated the table for me,where should i give value for the table? the automaticaly generated code can't be modified... – LogofaT Dec 02 '12 at 16:23
  • I don't know which code Netbeans generated, and how you customized it, but there must be a way to specify the model that the table must use. I would advise against using a wysiwyg editor like Netbeans, especially if you don't know how Swing works. Start by learning how to do everything by yourself, and then you'll understand how the editor could help (or not), and how to customize the code it produces. – JB Nizet Dec 02 '12 at 16:37
  • when i create the table like this : SeccondaryTable.setModel(new AngajatTable(aux)); aux is a variable that changes value when i click on a button, how should i reload the table? – LogofaT Dec 02 '12 at 21:01
  • You need to make the model fire the appropriate change event. Look at its fireXxx methods in the javadoc. – JB Nizet Dec 02 '12 at 21:39
0

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.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256