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.

2 Answers 2

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();
    }
    ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

This related example illustrates using a Map.
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...
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.
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?
You need to make the model fire the appropriate change event. Look at its fireXxx methods in the javadoc.
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.

Comments

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.